Validating XML file using XSD in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Validating XML file using XSD in UNIX
# 1  
Old 04-23-2014
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
Code:
<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
Code:
<?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:
Code:
<?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 :
Code:
<?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
# 2  
Old 04-24-2014
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.
# 3  
Old 04-24-2014
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?
# 4  
Old 04-25-2014
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

problem with xsd file creation

Hi every one, I am new to xml data files,I have two xml files with same data but only small difference as shown below <List> <number>1101</number> <Area>inner walls in a room.</Area> <Detection>less pressure.</Detection> <reason> <normal> <Component Num="15"... (1 Reply)
Discussion started by: veerubiji
1 Replies

2. Shell Programming and Scripting

How to: Validate a CSV file using an XSD?

Hi All, I was wondering if there is a utility/perl library/way of validating the contents of a CSV file using an XSD. i.e. Customer csv (including Header & Trailer) Cust_num, Cust_nme, Cust_typ, Cust_act_dte, Cust_loc, 101,Joe's Pizza,Retail,10121979,Detroit, 102,Sony... (6 Replies)
Discussion started by: luinzi
6 Replies

3. Shell Programming and Scripting

Validate xml agaist xsd is ksh

how do i validate xml agaist xsd is ksh? (1 Reply)
Discussion started by: LiorAmitai
1 Replies

4. Shell Programming and Scripting

Validating XML against XSD

Hi Frnds I want to validate an xml file against its xsd using KSH. But dont have any idea on how to proceed:confused:.. Please help me with your ideas.. Thanks in advance.... (3 Replies)
Discussion started by: balesh
3 Replies

5. UNIX for Dummies Questions & Answers

XML / XSD: what is a good format for amounts?

Suppose I have a XSD definition of amount: <xs:element name="Amount" type="amount> And the amounts themselves are given in the following format in the XML: <amount>1.000.00</amount> In other words, the thousand separator and the decimal point are the same. Does this require that the parser... (3 Replies)
Discussion started by: figaro
3 Replies

6. Shell Programming and Scripting

Help required converting XSD to XML file in PERL

Hi, Please find below the xsd. <?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="fruitorder"> <xs:complexType> <xs:sequence> <xs:element name="orderperson"... (2 Replies)
Discussion started by: vanitham
2 Replies

7. Shell Programming and Scripting

XMLLINT COMMAND IN UNIX TO VALIDATE XML AGAINST XSD

Hi i am baby to unix shell script. how do i validate xml agaist xsd and transforms xml using xslt. Thanks Mohan (2 Replies)
Discussion started by: mohan.cheepu
2 Replies

8. Shell Programming and Scripting

Validating Separator in unix

Hi I have an input CSV file which contains some values Format of file is as follows: 32R01000000003144476;32R01000000029054270;1000;6000;12/12/2007;0382521276;REG;Régulari I just need to validate it whether it is ';' seperated ie (if any other seperator is used it shld give an error) How do... (5 Replies)
Discussion started by: theeights
5 Replies

9. UNIX for Advanced & Expert Users

Generating XML from XSD

Hi all, Am looking for a way to generate XML based on XSDs so that the final XML need not be validated against the XSDs again. Here is the part of the XSD <xsd:element name="first_element"> <xsd:complexType> <xsd:sequence> <xsd:element name="val"/> </xsd:sequence> ... (0 Replies)
Discussion started by: matrixmadhan
0 Replies

10. UNIX for Dummies Questions & Answers

Validating XSL sheet data in Unix Data file

Dear All, Need your help. In my day to day activities I have to validate/search Excel Sheet data (eg.say Application No. 0066782345) data into the Unix environment file whether the same data is present in that file or not. There are hundreds of records coming in excel file and I am doing grep... (1 Reply)
Discussion started by: ravijunghare
1 Replies
Login or Register to Ask a Question