Friday 14 December 2012

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.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





13 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
  7. yes - why won't it show the line number for each validation error? that would be quite essential in fixing the problems.

    ReplyDelete
  8. I'm also getting the "Unable to parse schema file" error and the XSDs are all accessable and in the right paths. BTW my schemas use external schemas, also accessable, like:

    Any clues?

    ReplyDelete
  9. Thanks for this wonderful blog . awesome Article Really i have searching this type of valuable information From a lot of days i found satisfaction when read your blog Thanks for giving this type blog. very nice blog love it, thanks for sharing it with us really appreciate your work. notepad++ portable

    ReplyDelete
  10. Hi, when I validate the xml file using xml tools, it shows one error(highlighting that part of the file). It is not showing all the errors in the file. what am i supposed to do display all the errors in the popup window? Thanks !

    ReplyDelete
  11. Thank you! This was very helpful to me.

    ReplyDelete