473,832 Members | 2,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

default namespace and xpath

Hi;

I set up my xml as follows:
XmlDocument xml = new XmlDocument();
xml.Load(File.O pen("data.xml", FileMode.Open, FileAccess.Read ));
XmlNamespaceMan ager context = new XmlNamespaceMan ager(xml.NameTa ble);
context.AddName space("", "http://www.test.org");
context.AddName space("sns", "http://www.test.org/sub");
XmlNode node = xml.SelectSingl eNode("/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.AddName space("d", "http://www.test.org");
context.AddName space("sns", "http://www.test.org/sub");
XmlNode node = xml.SelectSingl eNode("/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 2411
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
2847
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> <size>345</size> <title>whatever</title> </module>
2
9870
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" xmlns="mailto:neo_in_matrix@msn.com?subject=ssmprjx" > <pr>
1
6855
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 "clear" with XPath Expression. XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml( xmlText );
3
1225
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 version="1.0" standalone="yes"?>
11
4342
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
2381
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"?> <StyledLayerDescriptor version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd"
7
1847
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 essentially following up to my last post so apologies for repeating parts of the post. The transformation stylesheet is (after corrections from kind contributors :) <?xml version="1.0" encoding="UTF-8"?>
3
4981
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 version="1.0" encoding="utf-8" ?> <thing xmlns="urn:thing-schema-v1"> <foo>foo thing</foo> <bar>bar thing</bar>
1
2235
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 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). It is an error...
0
9642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10499
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10541
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10212
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3972
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3078
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.