# Fix XML validation errors at the right layer

Source: https://xsdhub.com/learn/fix-xml-validation-errors

Debug malformed XML, missing child elements, invalid numeric values and namespace mismatches. Practical examples explain what to inspect before changing data.

## Start with the first syntax error

A missing closing tag can make several later lines look wrong. For <order><item>Notebook</order>, close item before order. Read the earliest parser diagnostic first, inspect the surrounding opening and closing tags, and validate again after each correction. Line numbers identify where the parser noticed a problem; the cause can be on an earlier line.

## Expected a number, found text

When a schema declares an element as xs:decimal or xs:positiveInteger, words and formatted currency labels are not accepted as those numeric types. For a decimal field, 24.50 can be valid while EUR 24.50 is not. Do not silently replace an unknown value with zero: obtain the intended value from the source system.

## Missing child element or unexpected element

An xs:sequence defines child order as well as which children can occur. If the schema expects customer followed by items, placing items first can trigger an error even when customer exists later. Compare the document with the relevant complexType. Check minOccurs and maxOccurs before adding or removing repeated elements.

## No matching global declaration

A root element may have the right local name but the wrong namespace. Compare its namespace URI to targetNamespace in the XSD. Also confirm that you selected the right schema version. Copying a namespace prefix from an example is not enough if it is bound to a different URI.

## A schema that cannot be compiled

Distinguish an invalid document from an invalid or unsupported schema. Check that the schema itself is well-formed XML and declares the XML Schema namespace. If it relies on includes or imports, load every referenced schema in the project loader and select the main schema. Preserve relative folder paths. Missing dependencies are errors; do not delete schema references to weaken validation.

## Retest the complete document

After fixing the source of an error, validate the complete document again and keep the result with the schema version used. A successful XSD check does not verify arithmetic totals, business identifiers or external regulatory rules unless the supplied schema actually expresses them.

## Primary references

- [W3C XML Schema primer](https://www.w3.org/TR/xmlschema-0/)
- [libxml2-wasm validation engine](https://github.com/jameslan/libxml2-wasm)
