# cvc-complex-type.2.4.a: Invalid content was found starting with element

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

> Java found an element where the schema expects a different one: out of order, misspelt, or in another namespace.

The message, as Java reports it: `cvc-complex-type.2.4.a: Invalid content was found starting with element 'qty'. One of '{id}' is expected.`

## Why it happens

- Elements in a different order than the schema's `xs:sequence`: the order matters, and the message names the element that came too early.
- A namespace mismatch, the most common cause when the names look identical. Xerces writes a namespaced name as `"urn:example":id` inside the braces: if the expected names carry a namespace and the element in the document has none (a missing `xmlns="…"`), or the other way round, they don't match.
- A misspelt name, a different letter case, or an element the schema doesn't allow at that point at all.

## How to fix it

- Read the list in braces: it is everything the schema would accept at that point. If one of them is in the document further down, move it up; if it is missing, add it; if the names look the same, compare their namespaces. With JAXB, check the `namespace` of `@XmlRootElement` and `@XmlElement` and the `elementFormDefault` in `package-info.java`.
- 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 [This element is not expected](https://xmlvalid.com/errors/this-element-is-not-expected): `Element 'qty': This element is not expected. Expected is ( id ).`

## Example that fails

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

## Fixed

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