You are on page 1of 5

VALIDATE XML AGAINST AN XSD USING NOTEPAD++

The application Notepad++ is free to download. A plugin called xml tools is also free to download.
One feature of the plugin is the ability to validate XML against an XSD.

As an example, if we have the following xsd saved in a file

<?xml version="1.0" encoding="UTF-8"?>


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">  
<xs:element name="userdetails">    
<xs:complexType>      
<xs:sequence>            
<xs:element name="username" type="xs:string"/>        
<xs:element name="password"  type="xs:string"/>
</xs:sequence>    
</xs:complexType>  
</xs:element>
</xs:schema>
and the following xml saved in another file

<userdetails>
   <username>Fred</username>
   <password>pass123</password>
</userdetails>
we can open the xml file in Notepad++. Once opened the XML can be validated against the XSD by
clicking on Plugins --> XML Tools --> Validate now
On clicking Validate now the dialogue box below will be displayed. Navigate to where the xsd is stored on
your hard drive and click OK.

If the xml validates against the xsd the following dialogue will be displayed.
If the xml is modified by adding an element which is not defined in the xsd

<userdetails>
 <username>Fred</username>
 <password>pass123</password>
 <newelement>dummy value</newelement>
</userdetails>
then when the validation is run an error is displayed

REFERENCING THE XSD WITHIN THE XML FILE

Instead of navigating to the xsd file the location can be specified in the xml document. In the example
below the xsd file is called simple_xsd_1.xsd and is located
at http://www.onsheld.co.uk/files/xml_files/simple_xsd_1.xsd
  
<userdetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="http://www.onsheld.co.uk/files/xml_files/simple_xsd_1.x
   <username>Fred</username>
   <password>pass123</password>
</userdetails>
VALIDATING XML WITH A NAMESPACE AGAINST AN XSD

The example below shows the same xml but with a default namespace of http://www.onsheld.co.uk/ns1
added

<?xml version="1.0"?>
<userdetails xmlns="http://www.onsheld.co.uk/ns1">
   <username>Fred</username>
   <password>pass123</password>
</userdetails>
The xsd is amended to include the new namespace

<xs:schema elementFormDefault="qualified"
           targetNamespace="http://www.onsheld.co.uk/ns1"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="userdetails">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="username"/>
        <xs:element type="xs:string" name="password"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
The xml would require the following two tags to point the xml to the xsd and indicate validation was
required 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
This indicates xml validation
xsi:schemaLocation="http://www.onsheld.co.uk/ns1 http://www.onsheld.co.uk/files/xml_files/a.xsd"
This has two parts, first the namespace followed by a space followed by the name and location of the xsd
file (in this case the file is called a.xsd which resides on the web at
location http://www.onsheld.co.uk/files/xml_files/a.xsd  )

The revised xml would be:


<?xml version="1.0"?>
<userdetails xmlns="http://www.onsheld.co.uk/ns1"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.onsheld.co.uk/ns1 http://www.onsheld.co.uk/files/xml
   <username>Fred</username>
   <password>pass123</password>
</userdetails>
If the schema (xsd file) is not accessible then the unable to parse schema file error is returned

You might also like