I was having a bear of a time today trying to figure out some inconsistent
behavior selecting nodes from and MSXML DOM document, so I distilled the issue
down to a trivial test demonstrating the confusing behavior.
The deal is that I want to create most of my XML nodes from code using
createNode, but the code is much more clear if I can start by building the
basic skeleton of the document using a simple XML string. The catch is, I'll
need to use namespaces.
== First - my test code output ==
name=root namespaceURI=uri:foo
name=child1 namespaceURI=uri:foo
name=value namespaceURI=
== Now - the code that makes that output ==
Public Sub TestDomFromString()
Dim objDoc As New MSXML2.DOMDocument40
objDoc.setProperty "SelectionNamespaces", _
"xmlns:foo='uri:foo'"
objDoc.setProperty "SelectionLanguage", "XPath"
objDoc.loadXML _
"<root xmlns='uri:foo'>" & _
"<child1 value='123'/>" & _
"</root>"
PrintNodeProfile objDoc.selectSingleNode("/*")
PrintNodeProfile objDoc.selectSingleNode("/*/*[1]")
PrintNodeProfile objDoc.selectSingleNode("/*/*[1]/@*")
End Sub
Public Sub PrintNodeProfile(Node As MSXML2.IXMLDOMNode)
Debug.Print "name=" & Node.nodeName & " " & _
"namespaceURI=" & Node.namespaceURI
End Sub
== End of code ==
It looks like MSXML automatically trats child elements as being in the parent
namespace as expected, but the unqualified attribute of an element in a
namespace has no namespace! needless to say, that was leading to some
confusion earlier today while debugging my selectNode results.
Now - the question is, is this an MSXML bug or quirk, or is there something I
just don't get yet about how a DOM loadXML call is supposed to behave? If
this is a problem with MSXML, how do I work around it. If I'm doing it wrong,
how should I do it right?
I have another issue that builds off that one. I notice that when I do a
selectNodes or SelectSingleNode, if I'm trying to select an attribute that
does have a namespace, I have to prefix the attribute name, but I don't have
to do that in XSLT nor when building key and keyref expressions in W3C XML
Schemas. Why is the MSXML selectNodes behavior different than these cases.
Thanks,
- Steve J.