Microsoft XML 2.5 SDK


 

XML Schemas and the DOM

[This is preliminary documentation and subject to change.]

Using the extension mechanism described in the section Extensibility, additional constraints can be specified that can be used by applications that process the data corresponding to the grammar defined in the schema. When you use XML Schema, the XML DOM provides a property on each element and attribute to quickly access its definition in the schema. The definition property on the XMLDOMNode object returns the corresponding ElementType or AttributeType from the schema document. Recall that because an XML schema document is itself an XML document, the schema can be parsed into a tree of nodes like any other XML document. The XML DOM can then be used to navigate the schema.

For example, 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" />
  <ElementType name="book" model="closed">
    <element type="title" />
    <element type="author" />
    <element type="pages" />
  </ElementType>
</Schema>

Book.xml

<?xml version ="1.0"?>
<book xmlns="x-schema:bookschema.xml" >
  <title>Presenting XML</title>
  <author>Richard Light</author>
  <pages>334</pages>
</book>

Start by getting the root element (assuming the document has been parsed, and the object reference doc refers to the parsed document):

var bookNode =  doc.documentElement;

You can now use the bookNode definition property to get the ElementType definition for the <book> element.

var bookDef = bookNode.definition;

bookDef is the returned schema declaration, or definition, of the <book> element:

<ElementType xmlns="urn:schemas-microsoft.com:xml-data" name="book" model="closed">
   <element type="title"/>
   <element type="author"/>
   <element type="pages"/>
</ElementType>

With bookDef, you can then access the extended information present in the schema.

var minPages = pagesET.childNodes(1).text;
var maxPages = pagesET.childNodes(2).text;