Visual Basic XSD Extension Function Code

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Reference

Visual Basic XSD Extension Function Code

The following code snippet shows how you can use XSD extension functions to return nodes based on their XSD data type. This example code uses the type-is function to return the first string data type in the document and the type-local-name function to return a list of nodes of the string data type.

The Visual Basic code creates an XMLSchemaCache50 object, adds the namespace URI declaration (urn:books) and the schema (books.xsd) to the object, and then references the schema using the schemas property of the DOMDocument object. Notice in the code that the SelectionLanguage property is set to XPath and the SelectionNamespaces property is set to "xmlns:ms='urn:schemas-microsoft-com:xslt" enabling the ms: namespace prefix to be used. the Validation is performed when the books.xml file is loaded into the DOMDocument object. Validation errors are returned using the parseError property of the DOMDocument object.

To run the example

  1. Copy the code shown below to the Command1_Click procedure. It should look like the code in the code snippet below when you are done.
  2. On the Visual Basic toolbar, click Start, and then click the Command1 button on form1.

When you run the example, the Set objNodeList = xmldom.selectNodes "//*[ms:type-is('http://www.w3.org/2001/XMLSchema','string')]") statement returns a list of nodes of data type string. The For/Next loop interates through the nodes and displays the text of each node in a message box.

Private Sub Command1_Click()
Dim xmlschema As Msxml2.XMLSchemaCache50
Set xmlschema = New Msxml2.XMLSchemaCache50
Dim xmldom As Msxml2.DOMDocument50
Set xmldom = New Msxml2.DOMDocument50
Dim objElem As IXMLDOMNode
Dim objNodeList As IXMLDOMNodeList

xmlschema.Add "urn:books", App.Path & "\books.xsd"
Set xmldom.schemas = xmlschema

xmldom.setProperty "SelectionLanguage", "XPath"
xmldom.setProperty "SelectionNamespaces", "xmlns:ms='urn:schemas-microsoft-com:xslt'"

xmldom.async = False
xmldom.Load App.Path & "\books.xml"

'returns a list of nodes with the string data type
Set objNodeList = xmldom.selectNodes _
    ("//*[ms:type-is('http://www.w3.org/2001/XMLSchema','string')]")
For i = 0 To (objNodeList.length - 1)
    MsgBox objNodeList.Item(i).Text
Next

If xmldom.parseError.errorCode <> 0 Then
    MsgBox xmldom.parseError.errorCode & " " & xmldom.parseError.reason
Else
    MsgBox "No Error"
End If

End Sub