# cvc-complex-type.4: Attribute must appear on element

Source: https://xmlvalid.com/errors/cvc-complex-type-4

> A required attribute is missing from an element.

The message, as Java reports it: `cvc-complex-type.4: Attribute 'currency' must appear on element 'order'.`

## Why it happens

- The attribute is declared `use="required"` and the document leaves it out.
- It is there under another name or letter case.
- Generated XML: JAXB leaves out an `@XmlAttribute` whose field is `null`.

## How to fix it

- Add the attribute with a valid value to the element the message names. When the XML is generated, set the field before marshalling, or give it a default.
- Java's schema validation (`javax.xml.validation.Validator`, and JAXB or Spring on top of it) throws at the first error unless you set an `ErrorHandler` whose `error()` records the `SAXParseException` and carries on; each one has `getLineNumber()` and `getColumnNumber()`. A well-formedness error ends parsing whatever the handler does.

## The same problem in xmlvalid

libxml2, which xmlvalid validates with, reports it as [The attribute is required but missing](https://xmlvalid.com/errors/attribute-is-required-but-missing): `Element 'order': The attribute 'currency' is required but missing.`

## Example that fails

```xml
<order>
  <id>A-1</id>
  <qty>1</qty>
</order>
```

## Fixed

```xml
<order currency="EUR">
  <id>A-1</id>
  <qty>1</qty>
</order>
```
