Validating the Documents Against Cached Schemas

only for RuBoard

You saw the validation against DTD in the last section's example; now it's time to see the validation against the XML Data Reduced (XDR) schema supported in MSXML 3.0. The IXMLDOMSchemaCollection interface enables you to cache parsed XDR schema definitions in memory and reuse them with different instances of the documents. This leads to improved efficiency and better performance because you do not have to load the XDR file each time you want to perform a validation. Listing 5.4 shows the two XDR files, Customers.xdr and Customer.xdr , the XML file Custdoc.xml , and the HTML file valschema.htm . Running the valschema.htm file in the browser shows you the validation against cached schemas in action.

Listing 5.4 Customers.xdr
 <?xml version="1.0" ?>  <Schema     xmlns = "urn:schemas-microsoft-com:xml-data"        xmlns:dt = "urn:schemas-microsoft-com:datatypes"        xmlns:cus="urn:my-Customer">       <ElementType name = "Customers" content="eltOnly" order="seq" >            <element type="cus:Customer"     />       </ElementType>  </Schema> 
Customer.xdr
 <?xml version="1.0" ?>  <Schema  xmlns = "urn:schemas-microsoft-com:xml-data"        xmlns:dt = "urn:schemas-microsoft-com:datatypes">        <ElementType name="Customer" content="eltOnly"       order="seq">                    <element type="CompanyName" minOccurs="1" maxOccurs="1"/>                    <element type="ContactName"  minOccurs="1" maxOccurs="1"/>        </ElementType>        <ElementType name="CompanyName" content="textOnly"      order="seq"/>         ElementType name="ContactName" content="textOnly"  order="seq"/>  </Schema> 
Custdoc.xml
 <?xml version="1.0" encoding="utf-8" ?>  <Customers xmlns="urn:my-Customers" xmlns:cus="urn:my-Customer">        <cus:Customer>            <cus:CompanyName>Alfreds Futterkiste</cus:CompanyName>            <cus:ContactName>Maria Anders</cus:ContactName>        </cus:Customer>  </Customers> 
valschema.htm
 <html>       <head>             <title>Schema Validation</title>       </head>       <body onload="schemaValidate()">       </body>  </html>  <script language="javascript">  function schemaValidate()  {      var objXML = new ActiveXObject("MSXML2.DOMDocument");       var schemas = new ActiveXObject("MSXML2.XMLSchemaCache");       schemas.add("urn:my-Customer", "customer.xdr");       schemas.add("urn:my-Customers", "customers.xdr");       objXML.schemas=schemas;       objXML.validateOnParse = false;       objXML.async = false;       objXML.load("custdoc.xml");       var parseError = objXML.validate();       if(  parseError.errorCode !=0 )       {           alert("The document is invalid. " + parseError.reason);       }       else       {           alert("The document is valid.");       }  }  </script> 

You can populate the schema collection with some schemas and attach it to the document through the schemas property ”defined in the IXMLDOMDocument2 interface. As shown in Listing 5.4 , the validate() method defined in the IXMLDOMDocument2 interface also allows you to perform run-time validation on the currently loaded document. Note that you can use this schema cache in another document's load method.

The MSXML 4.0 supports validation against W3C XSD schemas (Proposed Recommendation: March 30, 2001). Support for XDR will continue so that it is kept backwards compatible. So MSXML 4.0 supports validation using all the three validation types: DTD, XDR, and XSD.

only for RuBoard


XML and ASP. NET
XML and ASP.NET
ISBN: B000H2MXOM
EAN: N/A
Year: 2005
Pages: 184

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