XML Validation Using XML DOM


As XML documents become more and more pervasive as a standardized way to exchange data, there is an increasing need for the XML documents to be acceptable to different developers/users. To meet this need, the XML document should conform to a standard structure. One of the ways you can represent this standard structure is through XML Schema Definition (XSD) language. XML Schema is an XML-based representation of the structure of an XML document. Through its support for data types and namespaces, XML Schema has the potential to provide the standard structure for XML elements and attributes.

To determine whether an XML document conforms to an XML Schema, the document must be validated against that XML Schema. Through its support for XML validation, XML DOM allows you to validate XML through its properties.

Before looking at the code required to validate the XML document, create the Products.xsd file that will be used to validate the Products.xml file. (See Listing 12-5.)

Listing 12-5: Products.xsd schema

image from book
            <?xml version="1.0" encoding="utf-8"?>      <xs:schema xmlns="http://www.wrox.com/samples"        targetNamespace="http://www.wrox.com/samples" attributeFormDefault="unqualified"        elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">        <xs:element name="Products">          <xs:complexType>            <xs:sequence>              <xs:element maxOccurs="unbounded" name="Product">                <xs:complexType>                  <xs:sequence>                   <xs:element name="ProductID" type="xs:unsignedShort" />                   <xs:element name="Name" type="xs:string" />                   <xs:element name="ProductNumber" type="xs:string" />                  </xs:sequence>                  <xs:attribute name="Category" type="xs:string" use="required" />                </xs:complexType>              </xs:element>            </xs:sequence>          </xs:complexType>        </xs:element>      </xs:schema> 
image from book

To connect an XML schema to an XML document, you use an attribute named xsi:schemaLocation in the document element to specify the URI of the document's XML schema. To use this attribute so that Internet Explorer will understand it, you assign it a text string, giving the namespace you are using in your XML document, which is http://www.wrox.com/samples here, and the URI of the XML schema, which is Products.xsd in this case. If the Products.xsd file is in the same directory as that of the Products.xml file, you can set the xsi:schemaLocation attribute to http://www.wrox.com/samplesProducts.xsd. Here is the modified Products.xml file using the namespace http://www.wrox.com/samples.

      <?xml version="1.0" encoding="utf-8"?>      <Products xmlns="http://www.wrox.com/samples"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://www.wrox.com/samples Products.xsd">        <Product Category="Helmets">          <ProductID>707</ProductID>          ----          ----      </Products> 

Note that if you are not using a namespace in your XML document, you can use the xsi:noNamespaceSchemaLocation attribute and simply specify the location of the XSD.

Now that you have seen the XML and the corresponding XSD schema, Listing 12-6 shows the complete HTML page required for validating the Products.xml file with the Products.xsd file.

Listing 12-6: Validating an XML file with the XSD

image from book
            <html xmlns="http://www.w3.org/1999/xhtml">      <head>         <title>Validating an XML Document</title>         <script type="text/javascript" language="javascript">          var doc;          function btnValidate_Click()          {            loadDocument();          }          function loadDocument()          {            doc = new ActiveXObject("MSXML2.DOMDocument.6.0");            doc.resolveExternals = true;            doc.validateOnParse = true;            doc.async = false;            if (doc.load("Products.xml"))              document.write("Document is valid");            else              displayErrorInfo();          }          function displayErrorInfo()          {            document.write("Error code: " + doc.parseError.errorCode + "<br />");            document.write("Error reason: " + doc.parseError.reason + "<br />");            document.write("Error line: " + doc.parseError.line);          }          </script>      </head>      <body>        <input type="button"  value="Validate XML Document"          onclick="btnValidate_Click()" />      </body>      </html> 
image from book

Note the use of the validateOnParse property that is set to true to indicate the parser that the XML document needs to be validated at the time of parsing. In addition, you also set the resolveExternals property of DOMDocument to true in order to use external XSD document for validation. If the XML document is compliant with the XSD schema, you get a message indicating that the Document is valid. If the document is not compliant, you get an error message indicating the details of the error code, reason, and line.




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net