HOW TO: Use the ms:type-is XPath Extension Function to Program the MSXML DOM in Visual Basic for Windows (307935)



The information in this article applies to:

  • Microsoft XML 4.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0

This article was previously published under Q307935

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 top

Description 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 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 called 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 6.0, create a new Standard EXE project. Form1 is created by default.
  2. Add a project reference to Microsoft XML, version 4.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 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.

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

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 KB307935 kbAudDeveloper