Sunday, February 2, 2014
XSD (XML Schema Definition) represent the content model or grammar of an XML document. XML schema defines the elements that form an XML document.Validation is the process of verifying if the XML document complies to the defined schema definition.

Let us consider a simple XML file and the corresponding schema definition.

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
</catalog>

The schema definition.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" 
xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="catalog">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="author"/>
              <xs:element type="xs:string" name="title"/>
              <xs:element type="xs:string" name="genre"/>
              <xs:element type="xs:float" name="price"/>
              <xs:element type="xs:date" name="publish_date"/>
              <xs:element type="xs:string" name="description"/>
            </xs:sequence>
            <xs:attribute type="xs:string" name="id" use="optional"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Java program to validate XML against the XSD

package com.sourcetricks.validate;

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class ValidateXML {

 public static void main (String[] args) {
  
  try {
   // SchemaFactory for schema language W3C XML Schema 1.0
   SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
   
   // Parse xsd a provides a schema object
   Schema schema = schemaFactory.newSchema(new File("resources/input.xsd"));
   
   // Processor to check XML is valid against schema
   Validator validator = schema.newValidator();
   
   // Validates the specified input
   validator.validate(new StreamSource(new File("resources/input.xml")));
   
  } catch ( SAXException e ) {
   // SAXException provides the validation errors 
   System.out.println(e.getMessage());
  } catch ( IOException e ) {
   e.printStackTrace();
  }
 }
}

Output

When there are errors we get a SAXException. For example try changing id to id1 in the attribute of input XML.
cvc-complex-type.3.2.2: Attribute 'id1' is not allowed to appear in element 'book'.

Try the SmallWebTools - Free online XML validator against XSD

0 comments :

Post a Comment

Contact Form

Name

Email *

Message *

Back to Top