Microsoft KB Archive/307941

= HOW TO: Use the ms:type-local-name XPath Extension Function to Program the MSXML DOM in Visual Basic for Windows =

Article ID: 307941

Article Last Modified on 10/27/2002

-

APPLIES TO


 * Microsoft XML Core Services 4.0
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q307941





IN THIS TASK
SUMMARY
 * Description of the ms:type-local-name 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-local-name XPath extension function to program the MSXML 4.0 Document Object Model (DOM) in Visual Basic.

back to the top

Description 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 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 called 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 6.0, create a new Standard EXE project. Form1 is created by default.</li> <li>Add a project reference to Microsoft XML, version 4.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-local-name(.)='AuthorType']&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-local-name extension function to identify all of the nodes of the user-defined complex AuthorType XSD data type.

</li> <li>Save and run the project.</li> <li>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.</li></ol>

back to the top

<div class="references_section">