XDR Schemas and the DOM
When you use XML-Data Reduced (XDR) schema on an instance document, the schema definition of the elements and attributes in the instance document can be accessed using the definition property of the Document Object Model (DOM). The definition property of the IXMLDOMNode returns the type declaration that corresponds to the node just as it does in an external schema. The definition property does not work with inline schemas. The definition property returns the element in the schema that declared the type for the current element or attribute. For an element, defintion will return an ElementType element; for an attribute, it will return an AttributeType element. Although the schema's DOM is read-only and cannot be modified, it can be traversed, using all the functionality normally exposed through the DOM.
Consider the following example schema (Bookschema.xml) and XML documents.
Bookschema.xml
<?xml version="1.0"?> <Schema xmlns="urn:schemas-microsoft-com:xml-data"> <ElementType name="title"/> <ElementType name="author"/> <ElementType name="pages"/> <AttributeType name="price"/> <ElementType name="book" model="closed"> <element type="title"/> <element type="author"/> <element type="pages"/> <attribute type="price"/> </ElementType> </Schema>
Book.xml
<?xml version ="1.0"?> <a:book price="$12" xmlns:a="x-schema:Bookschema.xml" > <a:title>Presenting XML</a:title> <a:author>Richard Light</a:author> <a:pages>334</a:pages> </a:book>
Return the root element, the a:book node.
var bookNode = doc.documentElement;
Use the definition property on bookNode to get the ElementType definition for the a:book element.
var bookDef = bookNode.definition;
The following code returns the following element.
<ElementType xmlns="urn:schemas-microsoft.com:xml-data" name="book" model="closed"> <element type="title"/> <element type="author"/> <element type="pages"/> </ElementType>
Using bookDef, you can then access other schema information.
The following code selects element (0) in the childNodes element.
var title = bookDef.childNodes(0);
The following code returns the definition for the title element.
<element xmlns="urn:schemas-microsoft.com:xmldata" type ="title">
The following code selects element (1) in the childNodes element.
var author = bookDef.childNodes(1).xml;
The following code returns the definition for the author element.
<element xmlns="urn:schemas-microsoft.com:xmldata" type ="author">
The validate method of the DOMDocument attempts to validate the XML document to the schema or schemas specified in the document.
The validateOnParse property of the DOMDocument controls the use of schemas for validation. Setting validateOnParse to False does not stop schemas from being loaded and processed. Doing this does prevent document validation, but the default attributes, ids and idrefs, and data types are all processed according to the schema.
Multiple schemas can appear in the same instance document as long as they conform to the ordering rules and their IDs do not conflict.
The resolveExternals property of the DOMDocument controls whether the parser resolves the document to an external schema. Setting resolveExternals to False will stop the parsing of "x-schema:"
namespaces.
The SchemaCache is an object that allows the programmatic loading of schemas and association of those schemas with a namespace URI. When a document sees a namespace URI and looks for a schema, the first place it looks is the SchemaCache associated with the document. Therefore, a SchemaCache can be used to override x-schema external references as well as x-schema:# internal references.