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=ur i:foo
name=child1 namespaceURI=ur i:foo
name=value namespaceURI=
== Now - the code that makes that output ==
Public Sub TestDomFromStri ng()
Dim objDoc As New MSXML2.DOMDocum ent40
objDoc.setPrope rty "SelectionNames paces", _
"xmlns:foo='uri :foo'"
objDoc.setPrope rty "SelectionLangu age", "XPath"
objDoc.loadXML _
"<root xmlns='uri:foo' >" & _
"<child1 value='123'/>" & _
"</root>"
PrintNodeProfil e objDoc.selectSi ngleNode("/*")
PrintNodeProfil e objDoc.selectSi ngleNode("/*/*[1]")
PrintNodeProfil e objDoc.selectSi ngleNode("/*/*[1]/@*")
End Sub
Public Sub PrintNodeProfil e(Node As MSXML2.IXMLDOMN ode)
Debug.Print "name=" & Node.nodeName & " " & _
"namespaceU RI=" & Node.namespaceU RI
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 SelectSingleNod e, 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.