Thursday, December 13, 2007

Validating xml and xsd with xmllint

Recently, I've been going through some tutorials on XSD. I always find the tutorials on W3Schools to be very help and they've come through on this subject. The information on their site is concise and to the point. While going through their tutorials, I wanted to find an easy way of testing what I'd learned. Preferably, this would involve a command line utility.

I found that xmllint provides the functionality I was looking for. I'd like to step through what I've been doing to test my XML/XSD validation.

1. Create the XSD file. I'll use an example from W3Schools. Save the following to a file named note.xsd:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">

<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>


2. Create the XML file. Again, we'll use an example from W3Schools. Save the following to a file named note.xml:

<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


3. Validate the XML against the Schema. You will need to have xmllint installed. I type the following on the command line for validation:

xmllint --noout --schema note.xsd note.xml

This gives me the following output:

note.xml validates


Now that we know what happens for passing validation, I'd like to show you a failing one. Let's change the order of the elements in our XML. This will cause the validation to fail because the order is explicit per our XSD. The following XML switches the to and from elements:

<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<from>Jani</from>
<to>Tove</to>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


This will give the following output when we run xmllint:

note.xml:5: element from: Schemas validity error : Element '{http://www.w3schools.com}from': This element is not expected. Expected is ( {http://www.w3schools.com}to ).
note.xml fails to validate


I'm a little disappointed with the error message that I receive, but I think I'll gain a better understanding of them as I use the tool more.