Validate XML using XSD

Validate XML against XSD

XSD(XML Schema Definition) contains set of grammatical rules, Which can be used to validate a given xml. Lets consider a sample XML file.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Projects.xsl"?>
<catalog>
    <model name="s3">
        <network>GSM</network>
        <launch>2012-10-12</launch>
        <os>Android</os>
        <version>4.1</version>
        <cpu>dual-core</cpu>
        <weight>111.5</weight>
        <sim>mini-sim</sim>
    </model>
    <model name="s5">
        <network>GSM</network>
        <launch>2013-10-12</launch>
        <os>Android</os>
        <version>4.4</version>
        <cpu>quard-core</cpu>
        <weight>140.5</weight>
        <sim>micro-sim</sim>
    </model>
</catalog>

Now lets create a XSD file for the above xml file. In the xsd file we should clearly mention the data types of all the elements and the attributes of the xml. As an example, if the data type of the value inside the network element is string, we should declare it as below. With that xsd declaration we can't use other data types than string as the value data type of the network tag. Otherwise it will not be a valid xml.
  <xsd:element name="network" type="xsd:string"/>
In the given xml we have both simple types and the complex types. Simple type does not have any other elements or the attributes related with the element and complex type have other elements and/or attributes. In the above xml model is a complex type. We should declare it in xsd as below.

<xsd:complexType>
            <xsd:sequence>
                <xsd:element name="model" minOccurs="0" maxOccurs="unbounded">
          </xsd:sequence> 
</xsd:complexType>

In the given xml the model element has used more than once. So we should define maximum number of occurrences as maxOccurs. We can define simple types as below.

<xsd:element name="network" type="xsd:string"/>

The complete xsd to validate the given xml is as below.

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="catalog">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="model" minOccurs="0" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="network" type="xsd:string"/>
                            <xsd:element name="launch" type="xsd:string"/>
                            <xsd:element name="os" type="xsd:string"/>
                            <xsd:element name="version" type="xsd:decimal"/>
                            <xsd:element name="cpu" type="xsd:string"/>
                            <xsd:element name="weight" type="xsd:decimal"/>
                            <xsd:element name="sim" type="xsd:string"/>
                        </xsd:sequence>
                        <xsd:attribute name="name" type="xsd:string"/>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Now we can write a simple java code to check whether the xml is valid or not against the xsd. I have used some predefined java classes for the purpose.

package com.trainings.source;

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;

/**
 * @author hasanthi
 *
 */
public class ValidateXML {

    public static void main(String[] args) {
        System.out
                .println("EmployeeRequest.xml validates against Employee.xsd? "
                        + validateXMLSchema("./resources/phones.xsd",
                                "./resources/phones.xml"));

    }

    /**
     * @param xsdPath
     *            path of the passed xsd document
     * @param xmlPath
     *            path for the passed xml document
     * @return true if validates the document false if not
     */
    public static boolean validateXMLSchema(String xsdPath, String xmlPath) {
        try {
            SchemaFactory factory = SchemaFactory
                    .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new File(xsdPath));
            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(new File(xmlPath)));
        } catch (SAXException e) {
            System.out.println(e.getMessage());
            return false;
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

}
           

Comments

Popular posts from this blog

Applying CORS Filter to wso2 Identity Server

JWKS endpoint of wso2 IS

DCR VS DCRM with WSO2 Identity server