473,386 Members | 1,710 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,386 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 2361
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: MoonStorm | last post by:
Hi guys, Please solve a puzzle I am trying to figure out for some time. Let's say I have a fragment stored inside a variable, for instance: <xsl:variable name="layoutSettings"> <module>...
2
by: Edward Yang | last post by:
My XML document has a default namespace specified by xmlns="some_url". Here it is: <?xml version="1.0" encoding="utf-8" ?> <ssmproject name="sample" server="sql"...
1
by: Seong-Tae Jeong | last post by:
for example, xml document is below, It has a default namespace "xmlns='qwer://test'". string xmlText = "<test xmlns='http://test'><clear/><clear/></test>"; I would like to select node list...
3
by: Peter Theill | last post by:
Hi, I need some help on XPath ... what's the expressions to get: 1. All "Employee" nodes? 2. Only "Employee" nodes from "urn:test1" namespace? The document is shown below. <?xml...
11
by: EAI | last post by:
Hi All, I have a XML of the following form <?xml version="1.0"?> <xxxx xmlns="http://xxx.xxx.com"> .... </xxxx> When I try to read xml using SelectSingleNode, I am getting exception
3
by: rrm | last post by:
Hi I am verifying a SLD implementation using xml, however I have som problem using xpath and default namespaces in vb.net Currently I have the following xml <?xml version="1.0"?>...
7
by: Bilal | last post by:
Hello all, I came across this problem while working out the bugs in my identity trasnformation stylesheets but sidestepped it for later to see if there is an easier/better solution. This is...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
1
by: Arndt Jonasson | last post by:
The definition of XPath 1.0 says "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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.