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

IVBMXNamespaceManager

Provides a convenient way to manage namespaces, namespace prefixes, and namespace URIs for an XML document. With IVBMXNamespaceManager, namespace contexts are organized as a stack of namespace declarations. Each push operation pushes a namespace declaration or set of namespace declarations onto the stack. Each pop operation discards everything from the top of the stack. Namespace declarations on the stack have both current and local context.

Current context
Set of all namespace declarations on the stack.
oNSMgr.declarePrefix("a", "urn:same uri");
oNSMgr.pushContext();
oNSMgr.declarePrefix("b", "urn:new uri");

The current context holds the prefixes "a" and "b".

Local context
Set of all namespace declarations from the top of the stack to the last pushContext method.
oNSMgr.declarePrefix("a", "urn:same uri");
oNSMgr.pushContext();
oNSMgr.declarePrefix("b", "urn:new uri");

The local context holds the prefix "b".

Example

The following example shows how to use IVBMXNamespaceManager to manage namespace declarations.

XML file (test.xml)

<?xml version="1.0" ?>
<x xmlns:a="http://www.example.microsoft.com/a" xmlns:b="http://www.example.microsoft.com/b">
   <y Name="a:firstele"/>
   <y ref="a:firstele"/>
   <y Name="b:firstele"/>
   <y ref="b:firstele"/>
</x>

Visual Basic form code (NSMgr.frm)

Option Explicit

Private Sub Form_Resize()
    ' Resize the text box to the size of the form.
    Text1.Width = Form1.Width - 350
    Text1.Height = Form1.Height - 750
End Sub

Private Sub Form_Load()
    ' Resize the text box to the size of the form.
    Text1.Top = 100
    Text1.Left = 100
    Text1.Width = Form1.Width - 350
    Text1.Height = Form1.Height - 750
    Text1 = ""
    ' Process the attributes in the nodes selected using XPath. 
    ProcessNodes "x/y[@Name]", "Name"
    ProcessNodes "x/y[@ref]", "ref"
End Sub

Private Sub ProcessNodes(strXPathQuery As String, sAttrName As String)
    Dim oNodeList As IXMLDOMSelection
    Dim curNode As IXMLDOMNode
    Dim xmldoc As New DOMDocument50
    Dim oNSMgr As New MXNamespaceManager50
    Dim n, x As Long
    Dim str, strName, strValue, strPrefix, strSuffix, strURI As String
    xmldoc.Load App.Path & "\test.xml"
    ' If the element nodes you are selecting are namespace-prefixed,
    ' be sure to set the following additional second-level
    ' DOM selection properties:
    ' xmldoc.setProperty "SelectionLanguage", "XPath"
    ' xmldoc.setProperty "SelectionNamespaces", _
                  "xmlns:a='http://www.example.microsoft.com/a'"
    Set oNodeList = xmldoc.selectNodes(strXPathQuery)
    ' Processes the nodes that have the named attribute specified.
    For n = 0 To (oNodeList.length - 1)
        Set curNode = oNodeList.Item(n)
        For x = 0 To (curNode.Attributes.length - 1)
            If curNode.Attributes.Item(x).nodeName = sAttrName Then
                strName = curNode.Attributes.Item(x).nodeName
                strValue = curNode.Attributes.Item(x).nodeValue
                strPrefix = Left(strValue, InStr(strValue, ":") - 1)
                strSuffix = Right(strValue, _
                               Len(strValue) - InStr(strValue, ":"))
                ' Uses the getURIFromNode method of MXNamespaceManager
                ' to get the URI from the declarations of the DOMDocument
                ' object. This URI is used for the extracted prefix of the
                ' current DOM selection.
                strURI = oNSMgr.getURIFromNode(strPrefix, curNode)
                ' Display the output for each namespace URI 
                ' lookup process.
                 str = "Attribute: " & strName & " [" & _
                            CStr(n) & "]" & vbCrLf & _
                       "Attribute name: " & strName & vbCrLf & _
                       "Namespace prefix: " & strPrefix & vbCrLf & _
                       "Namespace URI: " & strURI & vbCrLf & vbCrLf
                 Text1 = Text1 & str
                 str = ""
            End If
        Next
    Next
    ' Clear the DOM object.
    Set xmldoc = Nothing
End Sub

Try It!

  1. Copy the sample XML file, and paste it into a text file. Save the file as test.xml.
  2. Create a Standard EXE project in Visual Basic. Save the empty project as NSMgr.vbp to the same directory where you saved test.xml. Save the form file as NSMgr.frm.
  3. Create a reference to MSXML 5.0. To do this, select References... from the Project menu, and then check the box for Microsoft XML, v5.0.
  4. Double click the TextBox icon from the tools menu. A TextBox control will appear on the project's form named "Text1". In the Properties window, set the value for the ScrollBars property to "2 – Vertical" and the value of the MultiLine property to "True".
  5. Copy the Visual Basic form code above, and paste it into the code editor to replace whatever is already there.
  6. Execute the code by selecting Start from the Run menu.
  7. Verify that the output is similar to that listed in below. You can resize the form as needed to view the output text more easily.

Output

When you build and run the previous example in Visual Basic, you will see text output similar to the following. This demonstrates that for each XPath selected node list and named attribute, the namespace URI was looked up using MXNamespaceManager and the getURIFromNode method it provides.

Attribute: Name [0]
Attribute name: Name
Namespace prefix: a
Namespace URI: http://www.example.microsoft.com/a

Attribute: Name [1]
Attribute name: Name
Namespace prefix: b
Namespace URI: http://www.example.microsoft.com/b

Attribute: ref [0]
Attribute name: ref
Namespace prefix: a
Namespace URI: http://www.example.microsoft.com/a

Attribute: ref [1]
Attribute name: ref
Namespace prefix: b
Namespace URI: http://www.example.microsoft.com/b

Remarks

IMXNamespaceManager provides duplicate methods for C++ processing, when necessary, to avoid the use of BSTR objects for C++.

IVBMXNamespaceManager performs a search from the top of stack through all namespace declarations, until a declaration is found or until the bottom of the stack is reached.

All indexes are continuous and start from 0.

IMXNamespaceManager checks [in] arguments for prefixes to ensure a valid prefix name according to the W3C Namespaces in XML Recommendation. IMXNamespaceManager does not check URI arguments.

Versioning

MSXML 4.0 and later

Requirements

Implementation: msxml5.dll, msxml2.lib

[C/C++]

Header and IDL files: msxml2.h, msxml2.idl

Version-Dependent ProgID: MSXML2.MXNamespaceManager50

Version-Dependent CLSID: 88d969d6-f192-11d4-a65f-0040963251e5

Remarks

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

IVBMXNamespaceManager Members | IMXNamespaceManager Members | IMXNamespacePrefixes

Other Resources Other Resources

W3C Namespaces in XML Recommendation