471,079 Members | 948 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

default namespace and xpath

Hi;

I set up my xml as follows:
XmlDocument xml = new XmlDocument();
xml.Load(File.Open("data.xml", FileMode.Open, FileAccess.Read));
XmlNamespaceManager context = new XmlNamespaceManager(xml.NameTable);
context.AddNamespace("", "http://www.test.org");
context.AddNamespace("sns", "http://www.test.org/sub");
XmlNode node = xml.SelectSingleNode("/root", context);

Where the xml is:
<root xmlns="http://www.test.org" xmlns:sns="http://www.test.org/sub">
<data>
...
</data>
</root>

But I get back a node of null. Now I can change it to do the following:
...
context.AddNamespace("d", "http://www.test.org");
context.AddNamespace("sns", "http://www.test.org/sub");
XmlNode node = xml.SelectSingleNode("/d:root", context);

And placing in the d: makes it work. But why can't I do an xpath of "/root"?

--
thanks - dave
Nov 12 '05 #1
5 2205
Hi Dave,

This is by design in .NET that we have to specify the prefix for the node
in an XPath expression, even it is in the default namespace.

If you have an XPath expression with a qualified name e.g.
pf:element-name, then that prefix pf needs to be bound to a namespace URI,
otherwise the XPath expression can't be evaluated. Some tools automatically
provide a binding of prefixes to namespace URIs then fine, but the .NET API
requires you to define those binding yourself if you want to use XPath
expressions with qualified names.

As for elements in the default namespace it is certainly correct acording
to the specs that you can only select them with an XPath expression using a
prefix bound to that default namespace URI, if those tools return/select
nodes in a default namespace by using unqualified names without a prefix
then those tools do not implement XPath 1.0 as is specified.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #2
Hi;

I've just been through http://www.w3.org/TR/xpath and
http://www.w3.org/TR/REC-xml-names/ and I can't find anything that requires a
prefix for the default namespace.

I did not find anything that specifically said it was ok to have no prefix
but this (from the doc) seemed to sort-of say it:

5.2 Element Nodes
There is an element node for every element in the document. An element node
has an expanded-name computed by expanding the QName of the element specified
in the tag in accordance with the XML Namespaces Recommendation [XML Names].
The namespace URI of the element's expanded-name will be null if the QName
has no prefix and there is no applicable default namespace.

Could you please point me at this requirement?

--
thanks - dave
"Kevin Yu [MSFT]" wrote:
Hi Dave,

This is by design in .NET that we have to specify the prefix for the node
in an XPath expression, even it is in the default namespace.

If you have an XPath expression with a qualified name e.g.
pf:element-name, then that prefix pf needs to be bound to a namespace URI,
otherwise the XPath expression can't be evaluated. Some tools automatically
provide a binding of prefixes to namespace URIs then fine, but the .NET API
requires you to define those binding yourself if you want to use XPath
expressions with qualified names.

As for elements in the default namespace it is certainly correct acording
to the specs that you can only select them with an XPath expression using a
prefix bound to that default namespace URI, if those tools return/select
nodes in a default namespace by using unqualified names without a prefix
then those tools do not implement XPath 1.0 as is specified.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #3
David Thielen wrote:
I've just been through http://www.w3.org/TR/xpath and
http://www.w3.org/TR/REC-xml-names/ and I can't find anything that requires a
prefix for the default namespace.


"A QName in the node test is expanded into an expanded-name using the
namespace declarations from the expression context. This is the same way
expansion is done for element type names in start and end-tags except
that the default namespace declared with xmlns is not used: if the QName
does not have a prefix, then the namespace URI is null (this is the same
way attribute names are expanded)."

http://www.w3.org/TR/xpath#node-tests
--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #4
Hi;

That does say it - I don't see how I missed it.

Any idea why they do this - it seems counter-intuitive.

--
thanks - dave
"Oleg Tkachenko [MVP]" wrote:
David Thielen wrote:
I've just been through http://www.w3.org/TR/xpath and
http://www.w3.org/TR/REC-xml-names/ and I can't find anything that requires a
prefix for the default namespace.


"A QName in the node test is expanded into an expanded-name using the
namespace declarations from the expression context. This is the same way
expansion is done for element type names in start and end-tags except
that the default namespace declared with xmlns is not used: if the QName
does not have a prefix, then the namespace URI is null (this is the same
way attribute names are expanded)."

http://www.w3.org/TR/xpath#node-tests
--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com

Nov 12 '05 #5
Sorry, Dave. Not quite sure. It is designed so.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by MoonStorm | last post: by
2 posts views Thread by Edward Yang | last post: by
3 posts views Thread by Peter Theill | last post: by
11 posts views Thread by EAI | last post: by
3 posts views Thread by Jason Mobarak | last post: by
1 post views Thread by Arndt Jonasson | last post: by
reply views Thread by leo001 | last post: by

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.