This element is not expected (cvc-complex-type.2.4.a)

The message

libxml2 (Python lxml, PHP, xmllint, this site):

Element 'Z': This element is not expected. Expected is ( A ).

Java / Xerces:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'Z'. One of '{A}' is expected.
cvc-complex-type.2.4.d: Invalid content was found starting with element 'Z'. No child element is expected at this point.

What it means

The validator walked through the children of an element in the order the schema's xs:sequence (or xs:choice) prescribes and met an element it did not expect at that position. The element may be unknown to the schema, in the wrong order, repeated more often than maxOccurs allows, or in the wrong namespace.

Typical causes

Example

Schema:

<xs:sequence>
  <xs:element name="Name"/>
  <xs:element name="Price"/>
</xs:sequence>

Document:

<Product>
  <Price>10</Price>   <!-- expected: Name first -->
  <Name>Pen</Name>
</Product>

How to fix it

Read the Expected is ( … ) part of the message: it lists what the schema allows at this point. Move the element there, rename it, or validate against the schema version your file was written for.

Find every occurrence at once

Load your XML file and its schema in the XML Online Viewer: it lists all validation errors with line, path and the surrounding lines, jumps to each one in the tree, and exports the list to Excel to hand to whoever produces the file. The schema is detected from xsi:schemaLocation when the document names it.

Other errors: all XML validation errors explained.

Validate your XML file