SUMMARY
This step-by-step article describes how to use the
ms:type-is XPath extension function to program the MSXML 4.0 Document Object Model (DOM) in Visual Basic.
back to the topDescription of the ms:type-is XPath Extension Function
The XPath implementation in MSXML 4.0 introduces a set of XSD extension functions that can specify expressions that evaluate nodes based on their data type. The
ms:type-is XPath extension function can identify nodes of a specified data type (as defined in an associated XSD schema) in an XML document.
You must supply 2 parameters when you use the
ms:type-is XPath extension function:
- The namespace Uniform Resource Identifier (URI) for the data type against which a node's data type is to be evaluated.
- The local name of the data type.
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 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-is('http://www.w3.org/2001/XMLSchema','float')]")
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-is extension function to identify all of the nodes of the XSD float data type.
- Save and run the project.
- Click the command button when the form is displayed to run the XPath query and display the name or names of the matching nodes. The price element in Books.xml is the only node whose data type is XSD float. The name of this node (price) is displayed in the Visual Basic Immediate window when the code is run.
back to the top