# How to validate XML against XSD, step by step

Source: https://xsdhub.com/learn/validate-xml-against-xsd

Use a complete XML and XSD example to check required attributes, numbers and element order. Learn how to read a failed result and retest the fix.

## Start with the schema your receiver expects

An XSD check answers a specific question: does this document follow this schema? Before editing data, confirm the schema version with the receiving application. A similar-looking sample schema can accept the wrong document or reject a correct one. For this walkthrough, the order contains a positive quantity, a decimal price and a required id attribute.

## 1. Copy this complete XSD

Open the XSD validator and paste this schema in the XSD editor. It is self-contained and uses XSD 1.0, so it does not need a network download or another schema file.

```xml
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="quantity" type="xs:positiveInteger"/>
        <xs:element name="price" type="xs:decimal"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>
</xs:schema>
```

## 2. Add a matching XML document

Paste this document in the XML editor. Keep XML + XSD selected and choose Validate XML. The example passes because the child elements appear in the declared order, the values match their types and the id attribute is present.

```xml
<order id="A17">
  <quantity>3</quantity>
  <price>12.50</price>
</order>
```

## 3. Make one deliberate error

Replace the quantity value 3 with three and validate again. The document remains well-formed XML, but it fails the positiveInteger requirement. Correct the value in the source system rather than just hiding the diagnostic. Try removing id next: that is a separate schema error.

## 4. Read the first useful diagnostic

A line number points to where a problem was detected. Inspect the surrounding element and its declaration. If syntax parsing fails, repair the XML before interpreting schema errors. When a field is unexpected, check order, namespace and spelling before deleting it. Export the JSON validation report if you need to share the result with a colleague.

## 5. Know what a passing result leaves open

This schema does not calculate totals, check that an order exists, or verify that a price is commercially correct. A successful result only covers rules expressed in the supplied XSD. XSDHub supports UTF-8 XML inputs up to 10 MB and schemas up to 1 MB each and XSD 1.0 schemas with local imports/includes through the project loader; XSD 1.1 is available through the optional server engine with explicit upload consent.

## Primary references

- [W3C XML Schema primer](https://www.w3.org/TR/xmlschema-0/)
