RE: XmlDocument.SelectSingleNode XPathException

Tech-Archive recommends: Fix windows errors by optimizing your registry



Thanks for your reply Dave,

As you said:
===========
The "xmlns:sns="http://www.test.org/sub";>" in the xml itself provides the
necessary information to turn:
//sns:subItem[@sid="11"]
into:
//http://www.test.org/sub:subItem[@sid="11";]
===========

yes, the XML Node or fragment did include the namespace declaraion.
However, this doesn't means that our XPATH can only use "sns" to idenity
the "http://www.test.org/sub"; namespace. The "sns" is only an alias. So
the following two xml fragment should be identical (validate through schema)

#fragment A
<root xmlns:sns="http://www.test.org/sub";>
<sns:item>
.......
</sns:item>
</root>

#fragment B
<root xmlns:aaa="http://www.test.org/sub";>
<aaa:item>
.......
</aaa:item>
</root>

However, if we only use the

"//sns:item" xpath and haven't specify an prefix/ URI mapping, it
will only work( using the JAVA API you mentioned) in

fragment A , but not in fragment B( though framgment A is exactly the same
xml document as fragment B).

And when using .net's Xpath selection, since we specify the
XmlNamespaceManager which provide the prefix(in our xpath) ----
namespaceURI mapping , we can perform the xpath selection no matter the
namespace prefix is changed to anyone in the source xml document. Also,
the prefix in our XPATH can vary , such as :

//bbb:item using AddNamespace("bbb", "http://test.org/sub";)

//ccc:item using AddNamespace("ccc", "http://test.org/sub";)

Anyway, I think the prefix( in xpath ) is not simply an alias( it should
always has a namespaceURI mapped to it) , otherwise, the prefix will be
meaningless alone.

Just some of my understanding. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)







.



Relevant Pages

  • Re: SQL2005, validation, & XQuery
    ... I think you are experiencing occurence issue, not namespace. ... your xpath syntax is correct. ... The xml in my sample columns only *had* one node: ... Microsoft Online Community Support ...
    (microsoft.public.sqlserver.xml)
  • Re: Simple xpath query with default namespace
    ... query (a prefix that's no actually in the xml) in order for the library ... whether it's the default namespace. ... then when you have your XPath query you will need to tell XPath which Account ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem getting node
    ... And your xpath expression is not namespace aware. ... A quick work around would be to remove the namespace from your Xml to ...
    (microsoft.public.dotnet.xml)
  • Re: XmlDocument.SelectSingleNode() & namespaces - doesnt work
    ... understand the difference between no namespace and default namespace - but the 1.0 spec is a bit unclear on exactly what this means in practice. ... That's the most FAQ ever with XPath. ... Read "XML Namespaces and How They Affect XPath and XSLT" at ...
    (microsoft.public.dotnet.xml)
  • Re: Simple xpath query with default namespace
    ... In XML you may have one XML definition that has a node called <Account ... The namespace allows that since one could be part of the namespace ... then when you have your XPath query you will need to tell XPath which Account ... foreach(System.Xml.XmlNode xmlNode in xmlNodes) ...
    (microsoft.public.dotnet.languages.csharp)