Validating XML file using XSD in UNIX

Hi,

I have a xml file and a xsd file(xml schema file). Here using unix script i wanted to validate the xml file by referring to xsd file. The validation is in terms of Datatype,Field length and null values. If the data present in the xml file is not matching in terms of datatype,field length and null values which are specified in the xsd file , then the bad records should go into badfile and good validated records into goodfile.
XML and XSD files are given below :
XSD file : bookSchema.xsd

<xsd:schema  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="generic"  elementFormDefault="qualified" targetNamespace="generic">
   <xsd:element name="bookstore" type="bookstoreType"/>
   <xsd:complexType name="bookstoreType">
       <xsd:sequence maxOccurs="unbounded">
           <xsd:element name="book" type="bookType"/>
       </xsd:sequence>
   </xsd:complexType>
   <xsd:complexType name="bookType">
       <xsd:sequence>
           <xsd:element name="title" type="xsd:string"/>
           <xsd:element name="author" type="authorName"/>
           <xsd:element name="price" type="xsd:integer"/>
       </xsd:sequence>
       <xsd:attribute name="genre" type="xsd:string"/>
   </xsd:complexType>
   <xsd:complexType name="authorName">
       <xsd:sequence>
           <xsd:element name="first-name" type="xsd:string"/>
           <xsd:element name="last-name" type="xsd:string"/>
           <xsd:element name="age" type="xsd:integer"/>
       </xsd:sequence>
   </xsd:complexType>
</xsd:schema> 

XML file : books.xml

<?xml version="1.0"?>
<bookstore xmlns="generic">
   <book genre="autobiography">
       <title>The Autobiography of Benjamin Franklin</title>
       <author>
           <first-name>Ben</first-name>
           <last-name>Franklin</last-name>
           <age>35</age>
       </author>
       <price>89</price>
   </book>
   <book genre="fiction">
       <title>Harry Potter</title>
       <author>
           <first-name>Joseph</first-name>
           <last-name>Melville</last-name>
         <age>abc</age>
       </author>
       <price>300</price>
   </book>
<book genre="novel">
       <title>2 States</title>
       <author>
           <first-name>Chethan11</first-name>
           <last-name>Bhagath</last-name>
        <age>35</age>
       </author>
       <price>150</price>
   </book>
<book genre="autobiography">
       <title>Wings of Fire</title>
       <author>
           <first-name>Abdul</first-name>
           <last-name>Kalam</last-name>
       <age>58a</age>
       </author>
       <price>200</price>
   </book>
<book genre="autobiography">
       <title>Adventures of Tintin</title>
       <author>
           <first-name>George</first-name>
           <last-name>Zumby</last-name>
       <age>44</age>
       </author>
       <price>200</price>
   </book>
<book genre="novel">
       <title>Adventures</title>
       <author>
           <first-name>44444</first-name>
           <last-name>Zumby</last-name>
       <age>44</age>
       </author>
       <price>205</price>
   </book>
</bookstore> 

After validation expected result is
Goodfile:

<?xml version="1.0"?>
<bookstore xmlns="generic">
   <book genre="autobiography">
       <title>The Autobiography of Benjamin Franklin</title>
       <author>
           <first-name>Ben</first-name>
           <last-name>Franklin</last-name>
           <age>35</age>
       </author>
       <price>89</price>
   </book>
   <book genre="novel">
       <title>2 States</title>
       <author>
           <first-name>Chethan11</first-name>
           <last-name>Bhagath</last-name>
        <age>35</age>
       </author>
       <price>150</price>
   </book>
  <book genre="autobiography 123">
       <title>Adventures of Tintin</title>
       <author>
           <first-name>George</first-name>
           <last-name>Zumby</last-name>
       <age>44</age>
       </author>
       <price>200</price>
   </book>
</bookstore> 

Badfile :

<?xml version="1.0"?>
<bookstore xmlns="generic">
     <book genre="fiction">
       <title>Harry Potter</title>
       <author>
           <first-name>Joseph</first-name>
           <last-name>Melville</last-name>
         <age>abc</age>
       </author>
       <price>300</price>
   </book>
<book genre="autobiography">
       <title>Wings of Fire</title>
       <author>
           <first-name>Abdul</first-name>
           <last-name>Kalam</last-name>
       <age>58a</age>
       </author>
       <price>200</price>
   </book>
<book genre="novel">
       <title>Adventures</title>
       <author>
           <first-name>44444</first-name>
           <last-name>Zumby</last-name>
       <age>44</age>
       </author>
       <price>205</price>
   </book>
</bookstore> 

How this can be done using unix script ?

Regards,
Shree

You can probably use xmllint, which is part of libxml, if it is available on your platform as the core of your script.

Alternatively, if you have Java skills, you could use Apache Xerces.

Hi,

I came to know that xmllint is a command line tool for parsing a xml file using xsd and i need to install it on my ubuntu server. So using this am I able to validate the data based on datatype ,field length and null values?Ans also am i able to send the good records and error records to the different files ? If yes, how this can be done?

XSD does not understand "records". XSD can be used to validate documents. xmllint is not a full conforming implementation of XSD and so may or may not validate your document.

There is no facility in XSD to separate "good/valid records" from "bad/invalid" records. That is out of scope for XSD. To separate "records", you will have to used XSLT or some other technology.