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.
oNSMgr.declarePrefix("a", "urn:same uri"); oNSMgr.pushContext(); oNSMgr.declarePrefix("b", "urn:new uri");
The current context holds the prefixes "a" and "b".
pushContext
method.
oNSMgr.declarePrefix("a", "urn:same uri"); oNSMgr.pushContext(); oNSMgr.declarePrefix("b", "urn:new uri");
The local context holds the prefix "b".
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!
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
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.
MSXML 4.0 and later
Implementation: msxml5.dll, msxml2.lib
Header and IDL files: msxml2.h, msxml2.idl
Version-Dependent ProgID: MSXML2.MXNamespaceManager50
Version-Dependent CLSID: 88d969d6-f192-11d4-a65f-0040963251e5
To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button in the upper-left corner of the page.
IVBMXNamespaceManager Members | IMXNamespaceManager Members | IMXNamespacePrefixes
W3C Namespaces in XML Recommendation