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



The information in this article applies to:

  • Microsoft XML 4.0

This article was previously published under Q307947

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:
<?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 named Books.xsd that defines its structure.

back to the top

Create the Sample XSD Document

  1. 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>
  2. Save Books.xsd in the same folder in which you saved Books.xml.
back to the top

Create and Test the Visual Basic Project

  1. In Visual Basic version 6.0, create a new Standard EXE project. Form1 is created by default.
  2. Add a project reference to Microsoft XML, v4.0.
  3. Drag a command button onto Form1.
  4. 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-namespace-uri()='urn:books']")
    
    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.

  5. Save and run the project.
  6. 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.
back to the top

REFERENCES

For a complete list of all the XPath extension functions that are implemented in MSXML 4.0, see the "Microsoft XPath Extension functions" topic in the XPath reference section of the MSXML 4.0 Software Development Kit (SDK) documentation.

back to the top

Modification Type:MajorLast Reviewed:10/27/2002
Keywords:kbhowto kbHOWTOmaster KB307947 kbAudDeveloper