SUMMARY
This step-by-step article describes how to use the
ms:type-local-name XPath extension function to program the MSXML 4.0 Document Object Model (DOM) in Visual Basic.
back to the topDescription of the ms:type-local-name XPath Extension Function
The XPath implementation in MSXML 4.0 introduces a set of XSD extension functions that can specify expressions that evaluate nodes in an XML document based on their data type. The
ms:type-local-name XPath extension function can return the non-qualified name of a node's data type as defined in an associated XSD schema.
The node whose data type's nonqualified name is to be returned is passed as the input argument of the
ms:type-local-name XPath extension function. In the code sample in this article, the
ms:type-local-name XPath function is used to return the nonqualified data type name of every node in the Books.xml file one at a time as the DOM tree is traversed by the execution of the
selectNodes DOM method. Each node whose non-qualified data type name is returned as
AuthorType is appended to the
IXMLDOMNodeList interface that the
selectNodes method of the
DOMDocument object generates and returns.
back to the topCreate the Sample XML Document
Use Notepad to create and save an XML document named Books.xml that contains the following XML:
<?xml version="1.0"?>
<x:catalog xmlns:x="urn:books" xsi:schemaLocation='urn:books Books.xsd'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<book>
<author>Adams</author>
<State>NC</State>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</x:catalog>
Books.xml references an XSD schema document called Books.xsd that defines its structure.
back to the topCreate the Sample XSD Document
- Use Notepad to create an XSD schema document named Books.xsd that contains the following code:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:books" xmlns:b="urn:books">
<xs:element name="catalog" type="b:CatalogData"/>
<xs:complexType name="CatalogData">
<xs:sequence>
<xs:element name="book" type="b:bookdata" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="AuthorType">
<xs:restriction base="xs:string">
<xs:maxLength value="5"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="bookdata">
<xs:sequence>
<xs:element name="author" type="b:AuthorType"/>
<xs:element name="State" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="genre" type="xs:string"/>
<xs:element name="price" type="xs:float"/>
<xs:element name="publish_date" type="xs:date"/>
<xs:element name="description" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string"/>
</xs:complexType>
</xs:schema>
- Save Books.xsd in the same folder in which you saved Books.xml.
back to the topCreate and Test the Visual Basic Project
- In Visual Basic 6.0, create a new Standard EXE project. Form1 is created by default.
- Add a project reference to Microsoft XML, version 4.0.
- Drag a command button onto Form1.
- Paste the following code in the Click event procedure of the command button, and specify the path to Books.xml in the xmldom.Load statement:
Dim xmlschema As MSXML2.XMLSchemaCache40
Dim xmldom As MSXML2.DOMDocument40
Set xmldom = New MSXML2.DOMDocument40
xmldom.async = False
xmldom.setProperty "SelectionNamespaces", "xmlns:ms='urn:schemas-microsoft-com:xslt'"
xmldom.Load "<Path to Books.xml>"
Set nlist = xmldom.selectNodes("//*[ms:type-local-name(.)='AuthorType']")
Dim node As MSXML2.IXMLDOMNode
For Each node In nlist
Debug.Print node.nodeName
Next
The selectNodes method of the DOMDocument object specifies an XPath expression that uses the ms:type-local-name extension function to identify all of the nodes of the user-defined complex AuthorType XSD data type.
- Save and run the project.
- Click the command button to run the XPath query and display the name or names of the matching nodes. The author element in Books.xml is the only node whose data type is AuthorType. The name of this node (author) is displayed in the Visual Basic Immediate window when the code is run.
back to the top