Microsoft KB Archive/307947

= HOW TO: Use the ms:type-namespace-uri XPath Extension Function When Programming the MSXML DOM =

Article ID: 307947

Article Last Modified on 10/27/2002

-

APPLIES TO


 * Microsoft XML Core Services 4.0

-



This article was previously published under Q307947





IN THIS TASK
SUMMARY
 * Description of the ms:type-namespace-uri XPath Extension Function
 * Create the Sample XML Document
 * Create the Sample XSD Document
 * Create and Test the Visual Basic Project

REFERENCES



SUMMARY
This step-by-step article describes how to use the ms:type-namespace-uri XPath extension function when you program the MSXML Document Object Model (DOM).

back to the top

Description of the ms:type-namespace-uri XPath Extension Function
The XPath implementation in MSXML version 4.0 introduces a set of XML Schema Definition (XSD) extension functions that can specify expressions that evaluate nodes in an XML document based on the data type of the nodes. The ms:type-namespace-uri XPath extension function can return the namespace Uniform Resource Identifier (URI) that is associated with the XSD data type of the current node, or the first node in the node set that is provided as its input parameter. This function can run XPath queries in MSXML 4.0 DOM code to identify nodes whose data types are associated with a specified namespace URI. This article provides a code sample to demonstrate how to use the ms:type-namespace-uri XPath extension function when you program the MSXML 4.0 DOM in Microsoft Visual Basic.

back to the top

Create the Sample XML Document
Use Notepad to create and save an XML document named Books.xml that contains the following XML:   Adams NC XML Developer's Guide Computer 44.95      2000-10-01 An in-depth look at creating applications with XML.  Books.xml references an XSD schema document named Books.xsd that defines its structure.

back to the top

Create the Sample XSD Document
  Use Notepad to create an XSD Schema document named Books.xsd that contains the following code: 



 <xs:sequence> <xs:element name=&quot;book&quot; type=&quot;b:bookdata&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot;/> </xs:sequence> </xs:complexType>

<xs:simpleType name=&quot;AuthorType&quot;> <xs:restriction base=&quot;xs:string&quot;> <xs:maxLength value=&quot;5&quot;/> </xs:restriction> </xs:simpleType>

<xs:complexType name=&quot;bookdata&quot;> <xs:sequence> <xs:element name=&quot;author&quot; type=&quot;b:AuthorType&quot;/> <xs:element name=&quot;State&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;title&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;genre&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;price&quot; type=&quot;xs:float&quot;/> <xs:element name=&quot;publish_date&quot; type=&quot;xs:date&quot;/> <xs:element name=&quot;description&quot; type=&quot;xs:string&quot;/> </xs:sequence>

<xs:attribute name=&quot;id&quot; type=&quot;xs:string&quot;/>

</xs:complexType>

</xs:schema> </li> <li>Save Books.xsd in the same folder in which you saved Books.xml.</li></ol>

back to the top

Create and Test the Visual Basic Project
<ol> <li>In Visual Basic version 6.0, create a new Standard EXE project. Form1 is created by default.</li> <li>Add a project reference to Microsoft XML, v4.0.</li> <li>Drag a command button onto Form1.</li> <li> 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 &quot;SelectionNamespaces&quot;, &quot;xmlns:ms='urn:schemas-microsoft-com:xslt'&quot; xmldom.Load &quot;<Path to Books.xml>&quot;

Set nlist = xmldom.selectNodes(&quot;//*[ms:type-namespace-uri='urn:books']&quot;)

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-namespace-uri extension function to identify all the nodes in Books.xml whose data types are associated with the urn:books custom namespace URI.

</li> <li>Save and run the project.</li> <li>Click the command button to execute the XPath query and display the name or names of the matching nodes. The x:catalog, book, and author elements in Books.xml have custom data types (CatalogData, bookdata, and AuthorType) that are defined in Books.xsd and associated with the urn:books custom namespace URI. The names of these nodes (x:catalog, book, and author) are displayed in the Visual Basic Immediate window when the code is run.</li></ol>

back to the top

<div class="references_section">