It seems that every place I've worked at has used XML documents to organize information. At my current place of employment, we use XML documents for various things. Lately, I've been working in the world of XML without any predefined XSD or DTD to verify the XML against. This isn't necessarily a problem in our case because we are the only consumers of the documents.
Recently, I've been tasked with using some of the files. I remember previously running into a tool for inferring schema documents from XML. I had to jog my memory a little to find it, but I found it. The tool is called Trang. It's a great way to see a description of what the XML generalizes to. After you download the jar, you can run it like so:
java -jar trang.jar *.xml testing.xsd
This will examine all the XML files in the current directory and create a file named 'testing.xsd'.
This is also a great for coming up with the schema by creating example XML files. After the schema is generated you can go in a tweak it if you need to.
Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts
Tuesday, December 29, 2009
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:
2. Create the XML file. Again, we'll use an example from W3Schools. Save the following to a file named note.xml:
3. Validate the XML against the Schema. You will need to have xmllint installed. I type the following on the command line for validation:
This gives me the following output:
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:
This will give the following output when we run xmllint:
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.
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.
Subscribe to:
Posts (Atom)
