Tuesday, May 9, 2017

How to validate an xml document against a schema

STEP 1: Create the XML

To create a new XML file, start eclipse IDE and choose File->New->Other->XML file->Create XML file from XML template.
Model the data of interest in the XML file. In my case, I have created the following XML.

The teams.xml file

<?xml version="1.0" encoding="UTF-8"?>
<team>
 <name>the great team</name>
 <description>these team was recruited by google to develop a secret product</description>
 <developers>
  <developer role="frontend">
   <name>John Carrot</name>
  </developer>
  <developer role="backend">
   <name>Joshua Allock</name>
  </developer>
  <developer role="frontend">
   <name>Brendan Tich</name>
  </developer>
 </developers>
</team>

STEP 2: Generate the XSD

Using your favorite tool, generate the XML Schema from the XML instance.
In my case, I have found an online service that has generated the XSD below for me.

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="team" type="teamType"/>
  <xs:complexType name="developerType">
    <xs:sequence>
      <xs:element type="xs:string" name="name"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="role" use="optional"/>
  </xs:complexType>
  <xs:complexType name="developersType">
    <xs:sequence>
      <xs:element type="developerType" name="developer" maxOccurs="unbounded" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="teamType">
    <xs:sequence>
      <xs:element type="xs:string" name="name"/>
      <xs:element type="xs:string" name="description"/>
      <xs:element type="developersType" name="developers"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

STEP 3: Add a target namespace to the XSD

Using eclise IDE create a new XSD file. Copy and paste the schema into the new empty schema file and save it as teams.xsd
Eclipse prompt you to create a target namespace and add a namespace declaration with a prefix that binds to the target namespace's URI.
After choosing a target namespace, the XSD schema looks like the one below:

The teams.xsd file

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.software-demon.org/teams" xmlns:tns="http://www.software-demon.org/teams"
  attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="team" type="tns:teamType" />
  <xs:complexType name="developerType">
    <xs:sequence>
      <xs:element type="xs:string" name="name" />
    </xs:sequence>
    <xs:attribute type="xs:string" name="role" use="optional" />
  </xs:complexType>
  <xs:complexType name="developersType">
    <xs:sequence>
      <xs:element type="tns:developerType" name="developer" maxOccurs="unbounded" minOccurs="0" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="teamType">
    <xs:sequence>
      <xs:element type="xs:string" name="name" />
      <xs:element type="xs:string" name="description" />
      <xs:element type="tns:developersType" name="developers" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

STEP 4: Qualify XML elements with a namespace

Now that the schema's element declarations belong to a target namespace, you have to slightly modify the XML file to qualify the elements in your XML file. In my case I have added a default namespace declaration.
Afterward, the XML file looks like the one below:

<?xml version="1.0" encoding="UTF-8"?>
<team xmlns="http://www.software-demon.org/teams" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.software-demon.org/teams teams.xsd ">
  <name>the great team</name>
  <description>these team was recruited by google to develop a secret product</description>
  <developers>
    <developer role="frontend">
      <name>John Carrot</name>
    </developer>
    <developer role="backend">
      <name>Joshua Allock</name>
    </developer>
    <developer role="frontend">
      <name>Brendan Tich</name>
    </developer>
  </developers>
</team>

Eventually, enable validation in eclipse from the menu Windows->Preferences. Now, eclipse seeks the teams.xsd schema to validate the team.xml instance against the schema.

No comments :

Post a Comment