Friday, 14 December 2012

Validate XML against an XSD using Notepad++







When developing an XML application it is often useful to validate the XML outside of the Oracle database. 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.xsd">
   <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_files/a.xsd">
   <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


6 comments:

  1. Hi

    I get an error "Unable to parse schema file. But i could actually open the schema file using NPP.

    Not sure why this is happening. Any help in this regard is appreciated.

    Let me know if any more details are required from my end.

    Thanks
    Jatin

    ReplyDelete
  2. Greate, It works for me.!!!...

    ReplyDelete
  3. these examples works and easily understand

    ReplyDelete
  4. In answer to Jatin, I ran into the same thing. I ran my XSD through an online validator found here:

    http://www.utilities-online.info/xsdvalidation/#.U2v-3flcWo0

    and it pointed out errors in my XSD file. Once fixed I was able to validate.

    Hope this helps.

    ReplyDelete
  5. Hi, this plugin is said to support validation against DTDs. But I couldn't find where to start this validation. I used your method for XSD validation to validate my XML file against the DTD but got this error Unable to parse schema file. Could you tell me how to validate XML against DTDs using this plugin? Thanks.

    ReplyDelete
  6. It's pretty useless with large XML when it doesn't show line numbers.

    ReplyDelete