Troubleshooting
XSD validation errors explained, with fixes
Most XSD validation errors fall into six groups: an element in the wrong place, a required child or attribute missing, an attribute that is not allowed, a value of the wrong type, a value outside an enumeration or pattern, and a root element the schema does not declare, usually because of a namespace mismatch. Each validator words them differently: xmllint says "This element is not expected", Java's Xerces says cvc-complex-type.2.4.a. This guide maps both to the fix.
1. The example schema and a document with seven errors
Every message on this page comes from validating the document below against order.xsd. The schema allows one customer followed by one or more items; each item needs a sku attribute matching three capital letters, a hyphen, and digits, then name, quantity, and price in that order. The order's status must be draft, submitted, or shipped.
Run it yourself with xmllint, which ships with macOS: xmllint --noout --schema order.xsd bad.xml.
<?xml version="1.0" encoding="UTF-8"?>
<!-- order.xsd -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:example:orders"
xmlns="urn:example:orders"
elementFormDefault="qualified">
<xs:element name="order">
<xs:complexType>
<xs:sequence>
<xs:element name="customer" type="xs:string"/>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="sku" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z]{3}-[0-9]+"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="status" default="draft">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="draft"/>
<xs:enumeration value="submitted"/>
<xs:enumeration value="shipped"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<!-- bad.xml -->
<order xmlns="urn:example:orders" status="pending">
<customer>Ada Lovelace</customer>
<item>
<name>XML Lens Pro</name>
<price>29.99</price>
</item>
<item sku="lens-2" colour="blue">
<name>Schema Guide</name>
<quantity>three</quantity>
<price>12.50</price>
<note>gift</note>
</item>
</order>2. What xmllint reports
xmllint prints one line per problem, each with the line number and the element in {namespace}name form, then "fails to validate". The sections below take these one at a time and give the Xerces wording you will see in Java, Eclipse, IntelliJ, and the VS Code XML extension.
$ xmllint --noout --schema order.xsd bad.xml
bad.xml:2: element order: Schemas validity error : Element '{urn:example:orders}order', attribute 'status': [facet 'enumeration'] The value 'pending' is not an element of the set {'draft', 'submitted', 'shipped'}.
bad.xml:4: element item: Schemas validity error : Element '{urn:example:orders}item': The attribute 'sku' is required but missing.
bad.xml:6: element price: Schemas validity error : Element '{urn:example:orders}price': This element is not expected. Expected is ( {urn:example:orders}quantity ).
bad.xml:8: element item: Schemas validity error : Element '{urn:example:orders}item', attribute 'sku': [facet 'pattern'] The value 'lens-2' is not accepted by the pattern '[A-Z]{3}-[0-9]+'.
bad.xml:8: element item: Schemas validity error : Element '{urn:example:orders}item', attribute 'colour': The attribute 'colour' is not allowed.
bad.xml:10: element quantity: Schemas validity error : Element '{urn:example:orders}quantity': 'three' is not a valid value of the atomic type 'xs:positiveInteger'.
bad.xml:12: element note: Schemas validity error : Element '{urn:example:orders}note': This element is not expected.
bad.xml fails to validate3. "This element is not expected" (cvc-complex-type.2.4.a)
An element appeared where the schema's content model does not allow it. In an xs:sequence, order matters, so the usual causes are a missing element before it, two elements swapped, a misspelled name, or an element in the wrong namespace. Read the expected list: it names what may come at that point.
In bad.xml, line 6 has price where quantity must come first. Insert the missing quantity, or move the elements into the schema's order. When nothing at all may follow, as with note on line 12, xmllint omits the expected list and Xerces uses cvc-complex-type.2.4.d.
- xmllint: Element 'price': This element is not expected. Expected is ( quantity ).
- Xerces: cvc-complex-type.2.4.a: Invalid content was found starting with element 'price'. One of '{quantity}' is expected.
- Xerces, nothing allowed: cvc-complex-type.2.4.d: Invalid content was found starting with element 'note'. No child element is expected at this point.
4. A required child or attribute is missing (cvc-complex-type.2.4.b, cvc-complex-type.4)
Elements without minOccurs="0" must appear, and attributes declared with use="required" must be present. When the element ends before its required children, xmllint says "Missing child element(s)"; when an attribute is absent, it says the attribute "is required but missing". Add the element or attribute with a valid value.
- xmllint: Element 'order': Missing child element(s). Expected is ( item ).
- Xerces: cvc-complex-type.2.4.b: The content of element 'order' is not complete. One of '{item}' is expected.
- xmllint: Element 'item': The attribute 'sku' is required but missing.
- Xerces: cvc-complex-type.4: Attribute 'sku' must appear on element 'item'.
5. An attribute is not allowed (cvc-complex-type.3.2.2)
The element has an attribute its type does not declare. Check the spelling against the schema (colour versus color is a classic), check whether the attribute belongs on a different element, or remove it. If the attribute should be allowed, the schema needs an xs:attribute or an xs:anyAttribute.
- xmllint: Element 'item', attribute 'colour': The attribute 'colour' is not allowed.
- Xerces: cvc-complex-type.3.2.2: Attribute 'colour' is not allowed to appear in element 'item'.
6. A value of the wrong type (cvc-datatype-valid, cvc-type.3.1.3)
The text does not match the declared type. Words where a number is expected, a comma as the decimal separator, a date not written as YYYY-MM-DD for xs:date, and an empty string in a numeric element are the usual causes. xs:positiveInteger also rejects 0. Xerces reports two lines for one value: the datatype rule, then the element or attribute that holds it.
- xmllint: Element 'quantity': 'three' is not a valid value of the atomic type 'xs:positiveInteger'.
- Xerces: cvc-datatype-valid.1.2.1: 'three' is not a valid value for 'integer'.
- Xerces: cvc-type.3.1.3: The value 'three' of element 'quantity' is not valid.
7. Not in the list, or not matching the pattern (cvc-enumeration-valid, cvc-pattern-valid)
An xs:enumeration allows only the listed values, compared exactly and case-sensitively. An xs:pattern is a regular expression that must match the whole value, so lens-2 fails [A-Z]{3}-[0-9]+ because the letters are lower case and there are only four. Use one of the listed values or rewrite the value to match the pattern. For attributes, Xerces adds a second line, cvc-attribute.3, naming the attribute.
- xmllint: attribute 'status': [facet 'enumeration'] The value 'pending' is not an element of the set {'draft', 'submitted', 'shipped'}.
- Xerces: cvc-enumeration-valid: Value 'pending' is not facet-valid with respect to enumeration '[draft, submitted, shipped]'. It must be a value from the enumeration.
- xmllint: attribute 'sku': [facet 'pattern'] The value 'lens-2' is not accepted by the pattern '[A-Z]{3}-[0-9]+'.
- Xerces: cvc-pattern-valid: Value 'lens-2' is not facet-valid with respect to pattern '[A-Z]{3}-[0-9]+' for type '…'.
8. "No matching global declaration" (cvc-elt.1.a)
The validator found no top-level declaration for the root element in the namespace it is in. Nine times out of ten the element names are right and the namespace is wrong: the document has no xmlns, or a different one, while the schema has a targetNamespace. Add xmlns="urn:example:orders" (the schema's exact targetNamespace) to the root element. Otherwise, check that the root element is declared globally, directly under xs:schema.
<!-- Fails: no namespace -->
<order status="draft">
<!-- Validates: the schema's targetNamespace -->
<order xmlns="urn:example:orders" status="draft">- xmllint: Element 'order': No matching global declaration available for the validation root.
- Xerces: cvc-elt.1.a: Cannot find the declaration of element 'order'.
9. A faster way to work through errors
Fix them top to bottom. One early mistake, especially a missing or misplaced element, can make the validator misread everything after it, so a single fix often clears several messages. Validate again after each change.
If the file is not well-formed, none of these messages appear: the parser stops first. Fix parse errors such as mismatched tags before you look at schema errors.
Common questions
What does cvc-complex-type.2.4.a mean?
An element appeared where the schema does not allow it. In a sequence, order matters: add the element named in "One of ... is expected" before it, or move the elements into the schema's order.
What does cvc-elt.1.a Cannot find the declaration of element mean?
The validator found no global declaration for the root element in its namespace. Usually the root element's namespace does not match the schema's targetNamespace; add the correct xmlns.
Why is my enumeration value rejected?
Enumeration values are compared exactly, including case and spaces. Use one of the values listed in the schema.
Why do I get two errors for one bad value in Java?
Xerces reports the datatype or facet rule first, then the element or attribute that holds the value. Both lines describe the same problem.
Why does fixing one error clear several?
After a misplaced or missing element the validator can misread the rest of the parent's content. Fix errors from the top and validate again.
References
W3C XML Schema Part 1: Structures (schema location hints) ↗W3C XML Schema Part 0: Primer ↗xmllint manual (libxml2) ↗Apache Xerces2 Java parser ↗XML extension for VS Code: validation ↗Try the workflow in XML Lens
XML Lens is a native Mac app for viewing, validating, querying and comparing XML that keeps your files on your Mac. It is listed as XML Editor Lens: XPath on the Mac App Store.
Download XML Lens for MacExplore the relevant Mac workspaces and their capabilities.
Validate XML and run local XSLT 1.0 on Mac Write valid XML as you type with an XSD on Mac View and map XML Schema (XSD) locally on Mac