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 definition property on bookNode 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 other schema information.
var title = bookDef.childNodes(0); //returns <element xmlns="urn:schemas-microsoft.com:xmldata" //type ="title"/> var author = bookDef.childNodes(1).xml; //returns <element xmlns="urn:schemas-microsoft.com:xmldata" //type ="author">