472,780 Members | 1,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 2317
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.