473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

MSXML 4.0 loadXML and namespaces - what's wrong with this picture

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.
Aug 18 '05 #1
5 4359
Steve Jorgensen wrote:
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.
the default namespace doesn't apply on attributes
unprefixed attributes are not is a namespace ; it is useless because
they are told to "belong" to their host element ; those that do have a
namespace are not belonging to it, they are "foreign attributes" and
must be prefixed

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
the behaviour is the same ; the XML data model is the same whatever the
tool you use, an XSLT stylesheet or an XPath engine

nor when building key and keyref expressions in W3C XML Schemas. Why is the MSXML selectNodes behavior different than these cases.

Thanks,

- Steve J.

--
Cordialement,

///
(. .)
-----ooO--(_)--Ooo-----
| Philippe Poulard |
-----------------------
Aug 18 '05 #2


Steve Jorgensen wrote:

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.


But that is nothing specific to MSXML, an attribute is in no namespace
unless it has a qualified name which the value attribute does not have,
it is in no namespace.
Exception is an xmlns="http://example.com/ns1" attribute but you would
not see that in XPath as an attribute but as a namespace node. In the
DOM however such an attribute is by definition in the namespace
http://www.w3.org/2000/xmlns/. Looks however as if MSXML 4 does not
implement that.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 18 '05 #3
On Thu, 18 Aug 2005 13:38:37 +0200, Martin Honnen <ma*******@yahoo.de> wrote:


Steve Jorgensen wrote:

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.


But that is nothing specific to MSXML, an attribute is in no namespace
unless it has a qualified name which the value attribute does not have,
it is in no namespace.
Exception is an xmlns="http://example.com/ns1" attribute but you would
not see that in XPath as an attribute but as a namespace node. In the
DOM however such an attribute is by definition in the namespace
http://www.w3.org/2000/xmlns/. Looks however as if MSXML 4 does not
implement that.


OK, what you're saying clears up a lot of things. I think I may be having a
different problem with MSXML and XML Schema validation, then, or I never would
have thought I had a problem with attributes being unqualified in the first
place. In any case, I'll proceed with diagnosis today with a much more useful
understanding.

Thanks very much to you and to Philippe for your explanations.
Aug 18 '05 #4
On Thu, 18 Aug 2005 07:05:19 -0700, Steve Jorgensen <no****@nospam.nospam>
wrote:

....
OK, what you're saying clears up a lot of things. I think I may be having a
different problem with MSXML and XML Schema validation, then, or I never would
have thought I had a problem with attributes being unqualified in the first
place. In any case, I'll proceed with diagnosis today with a much more useful
understanding.


OK, this turned out to be all just a series of my misconceptions. I had
declared one of the attributes in my schema globally, and I didn't realize
that forced it to be qualified. That was compounded by my lack of
understanding that when attributes are "unqualified", that really means they
have no namespace, and doesn't just mean that they don't need a prefix to be
in the parent element's namespace.
Aug 19 '05 #5
Philippe Poulard wrote:

Hi, forgive med for breaking in here, I think the OP's had his questions
answered anyway ;)
the default namespace doesn't apply on attributes
unprefixed attributes are not is a namespace ; it is useless because
they are told to "belong" to their host element ; those that do have a
namespace are not belonging to it, they are "foreign attributes" and
must be prefixed


That's the most normal way to use namespace-qualified attributes, yes...

From Namespaces in XML (http://www.w3.org/TR/REC-xml-names/)

5.2 Namespace Defaulting

A default namespace is considered to apply to the element where it is
declared (if that element has no namespace prefix), and to all elements
with no prefix within the content of that element. If the URI reference
in a default namespace declaration is empty, then unprefixed elements in
the scope of the declaration are not considered to be in any namespace.
Note that default namespaces do not apply directly to attributes.

On the other hand:

[Definition:] The attribute's value, a URI reference, is the namespace
name identifying the namespace. The namespace name, to serve its
intended purpose, should have the characteristics of uniqueness and
persistence. It is not a goal that it be directly usable for retrieval
of a schema (if any exists). An example of a syntax that is designed
with these goals in mind is that for Uniform Resource Names [RFC2141].
However, it should be noted that ordinary URLs can be managed in such a
way as to achieve these same goals.

I have a situation where I need to insert an element in no-namespace
land into a document where the default namespace is declared. Is it
permitted to do:

<foo xmlns="gedefims">
<blah/>
<blah/>
<blah/>
<namespaceless-intruder xmlns=""/>
</foo>

- It should be permitted because 5.2 in NS in XML says it is
but
- The empty string ain't no URI

Does anyone know if I can always expect it to work? At least one XML
validator complains about the empty URI. I know I can always get around
it the hard way -- using a prefix-declared namespace for foo and blahs
instead. But there's too much trouble with it .. for one thing, I also
have attribute VALUES being interpreted depending on the bindings of the
NSs.

Soren
Aug 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: isee | last post by:
I want to send xml to some site and get response (also XML). I am using vb.net,but I don't want to install MSXML 4.0 .
2
by: Andy Norman | last post by:
I have a strange problem. When I try to call the input property of the MSXML processor object from VBScript in an ASPX page I get the error "Member not found". The same code (with a few "set"...
3
by: awong | last post by:
Hi there, I was trying to convert the following VB6 code to VB.NET. But I can't find a corresponding System.XML object for MSXML IXMLDOMSelection. I am thinking to use System.XML XMLNodeList...
4
by: Tim Haughton | last post by:
I think I might be misunderstanding just what the LoadXml method is doing. I have 2 seemingly identical XmlDocuments, an XPath query succeeds on one of them, and fails on the other. Can anyone tell...
0
by: upendra.desai | last post by:
Hi, I have written an application in Microsoft Visual C++ (Visual Studio 6.0) that uses MSXML 4.0 SP2. When I try to load an XML from a file, it throws an E_ACCESSDENIED (HRESULT = 0x80070005)...
0
by: upendra.desai | last post by:
Hi, I have written an application in Microsoft Visual C++ (Visual Studio 6.0) that uses MSXML 4.0 SP2. When I try to load an XML from a file, it throws an E_ACCESSDENIED (HRESULT = 0x80070005)...
19
by: Matthias Truxa | last post by:
Hello, can anyone confirm the existence of the following effects which I'd consider being a critical bug in msxml according to w3c's xpath specs? The Spec says: "The parent, ancestor,...
6
by: Anthony Jones | last post by:
People, Anyone else got an IIS7 server out there that they can test this little ASP file:- <% Set xml = Server.CreateObject("MSXML2.DOMDocument.3.0") xml.loadXML "<root />" Set xsl =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.