WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

MSXML

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

XML Validate Using XSD File


Question:

We have a project that currently runs from Winbatch scripts to generate files for a large customer (thousands upon thousands a day). They want us to convert the output to XML and it must be validated against the schema they've developed. The tool we're using to generate the XML is Pervasive's Business Integrator (data transformation engine - formerly Data Junction) but I want to run each XML file generated from the Pervasive product through a validator to make sure it conforms to the XSD prior to giving it back to the customer (they don't take kindly to errors nor does our management!). If it passes, great, if not, it would flagged and dumped somewhere else to be dealt with.

Does anyone know of an XML validator product that will allow me to pass in the XML file and the XSD file and have it tell me whether it's valid or not? A command line interface would be great but OLE would work as well. This is all new to us here so I'm fishing for ideas/suggestions. The Pervasive software just does the transformation from the current file format to the XML but won't validate the output.

Answer:

Copy this XML data code and paste it into a text file. Save the file as sl-valid.xml.
<?xml Version="1.0"?>
<catalog xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
         xsi:schemaLocation='urn:book sl.xsd'>
   <x:book
      xmlns:x='urn:book'
      id="bk101">
      <x:author>Gambardella, Matthew</x:author>
      <x:title>XML Developer's Guide</x:title>
      <x:genre>Computer</x:genre>
      <x:price>44.95</x:price>
      <x:publish_date>2000-10-01</x:publish_date>
      <x:description>An In-depth look at creating applications with
      XML.</x:description>
   </x:book>
</catalog>
Copy this invalid XML data code and paste it into a text file. Save the file as sl-notValid.xml, in the same directory where you saved sl-valid.xml.
<?xml Version="1.0"?>
<catalog xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
         xsi:schemaLocation='urn:book sl.xsd'>
   <x:book
      xmlns:x='urn:book'
      id="bk101">
      <x:author>Gambardella, Matthew</x:author>
      <x:title>XML Developer's Guide</x:title>
      <x:genre>Computer</x:genre>
      <x:cost>44.95</x:cost>
      <x:publish_date>2000-10-01</x:publish_date>
      <x:description>An In-depth look at creating applications with
      XML.</x:description>
   </x:book>
</catalog>
Copy this XSD schema code and paste it into a text file. Save the file as sl.xsd, in the same directory you used in previous steps.
<?xml Version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:x="urn:book"
            targetNamespace="urn:book"
            elementFormDefault="qualified">
<!--  *** Add the following If attributes
          are To be namespace prefixed ***
            attributeFormDefault="qualified">
-->
  <xsd:element name="book">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="author" type="xsd:string"/>
      <xsd:element name="title" type="xsd:string"/>
      <xsd:element name="genre" type="xsd:string"/>
      <xsd:element name="price" type="xsd:float"/>
      <xsd:element name="publish_date" type="xsd:date"/>
      <xsd:element name="description" type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:string"/>
  </xsd:complexType>
  </xsd:element>
</xsd:schema>
Copy this WIL script and paste it into a text file. Save the file as schemaLocation.wbt, in the same directory you used in previous steps.
strFile = 'sl-valid.xml'
;strFile = 'sl-notValid.xml'

; Create an XML DOMDocument object.
;x = ObjectCreate("MSXML2.DOMDocument.5.0");
x = ObjectCreate("MSXML2.DOMDocument");

; Set first-level DOM properties.
x.async = @FALSE
x.validateOnParse = @TRUE
x.resolveExternals = @TRUE

; Configure DOM properties for namespace selection.
x.setProperty("SelectionLanguage", "XPath");
ns = "xmlns:x='urn:book'";
x.setProperty("SelectionNamespaces", ns);

;Load and validate the specified file into the DOM.
x.load(strFile);

; Return validation results in message to the user.
If x.parseError.errorCode != 0
   Message(StrCat("Validation failed on " , strFile ), StrCat(@CR, "Reason: ", x.parseError.reason, @CR, "Source: ", x.parseError.srcText, @CR,"Line: ", x.parseError.line))
Else
   Message(StrCat("Validation succeeded for " , strFile), x.xml )
EndIf
Now Run the schemaLocation.wbt file.


Article ID:   W17349
File Created: 2007:07:03:14:29:34
Last Updated: 2007:07:03:14:29:34