473,320 Members | 1,817 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,320 software developers and data experts.

Prefixed namespaces and Xpath

I have come across what seems like a failing in the .Net XML classes.
Many people have posted requesting how to write an XPath query with
namespace prefixes that works. The solution shown in all the posts I
have seen is of the form:

---------------------------------------------
XmlDocument doc = new XmlDocument();
doc.Load(@"c:\foobar.xml");
XmlNamepaceManager nmMgr = newNamespaceManager(doc);
nmMgr.AddNamespace("abc", "www.my.uri");

---------------------------------------------

Now you can call SelectSingleNode and SelectNodes with an XPath query
that contains the abc prefix.

What concerns me is that this manual registering of prefixes is
necessary even if the prefix was already defined in the source
document, e.g.

<abc foo xmlns:abc="www.my.uri"/>
Therefore the solution is only good if you are dealing with a known
set of uri's and prefixes. If (in my case) you are dealing with
arbitrary documents with arbitrary uri's and prefixes defined then the
solution is of no use.

AFAIK the only way to recognise prefixes declared within a document is
to write your own code to traverse the document, extract the
prefix/uri pairs and /then/ register them with an XmlNamespaceManager.

Thoughts?

Cheers,

Colin Green
Nov 12 '05 #1
3 2373
AFAIK the only way to recognise prefixes declared within a document is
to write your own code to traverse the document, extract the
prefix/uri pairs and /then/ register them with an XmlNamespaceManager.


Yes, why not? This is not difficult to do.

See, e.g.:

http://groups.google.com/groups?q=na...rlin.de&rnum=1
Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
Nov 12 '05 #2
Colin Green wrote:

What concerns me is that this manual registering of prefixes is
necessary even if the prefix was already defined in the source
document, e.g.

<abc foo xmlns:abc="www.my.uri"/>
Therefore the solution is only good if you are dealing with a known
set of uri's and prefixes. If (in my case) you are dealing with
arbitrary documents with arbitrary uri's and prefixes defined then the
solution is of no use.


That's wrong. Prefix doesn't matter. You can use
nmMgr.AddNamespace("foo", "www.my.uri");
with the same success (of course foo prefix then should be used in XPath
expressions).
What only matters is namespace URI.
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #3
Well you don't need to know the exact prefixes - but you do need to know the
Uri's. The prefixes are extended to full URI's anyway so if the prefix in
your document is "myns" associated with the uri http://someuri.com/ and in
your .Net class you associate the prefix "yourns" with the URI
"http:someuri.com/" then there is no difference. myns and yourns are
expanded to the same URI.

As for URI's - yes this is true. But this only makes sense. If you define an
Xml element within the scope of some URI you are effectively defining its
type. Although two elements may have the same name, if they ahve different
namesapces they are twop completely different things. Can you imagine in the
..Net world just creating an instance of "class1"? This is the default class
that is created with every new project in VS.Net - imagine you just
compliled all of them with no namespaces. Just a a whole bunch of "class1"'s
in the same directly. (Imagine...) If you were able to create an instance -
what class1 are you talking about? The key is that namespaces can be used to
differentiate the significance of different classes that have the same name.
For some reason in the Xml world people just don't seem to see this as being
effectively the same thing.

I guess Xml without namespaces would be like "DLL hell" on windows :)

Also, sure you can reflect against the Xml doc and get the namespaces and
build your NSManager from there. However, how often do you actually ever do
a SelectNode... without knowing the namespace (or type) of the node you are
selecting?

Well, you asked for my thoughts :)

Cheers,
Steven
"Colin Green" <bu*******@hotmail.com> wrote in message
news:a2**************************@posting.google.c om...
I have come across what seems like a failing in the .Net XML classes.
Many people have posted requesting how to write an XPath query with
namespace prefixes that works. The solution shown in all the posts I
have seen is of the form:

---------------------------------------------
XmlDocument doc = new XmlDocument();
doc.Load(@"c:\foobar.xml");
XmlNamepaceManager nmMgr = newNamespaceManager(doc);
nmMgr.AddNamespace("abc", "www.my.uri");

---------------------------------------------

Now you can call SelectSingleNode and SelectNodes with an XPath query
that contains the abc prefix.

What concerns me is that this manual registering of prefixes is
necessary even if the prefix was already defined in the source
document, e.g.

<abc foo xmlns:abc="www.my.uri"/>
Therefore the solution is only good if you are dealing with a known
set of uri's and prefixes. If (in my case) you are dealing with
arbitrary documents with arbitrary uri's and prefixes defined then the
solution is of no use.

AFAIK the only way to recognise prefixes declared within a document is
to write your own code to traverse the document, extract the
prefix/uri pairs and /then/ register them with an XmlNamespaceManager.

Thoughts?

Cheers,

Colin Green

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Piet | last post by:
Hello, Via Xpath, I want to access nodes which have a namespace prefix. THe document at hand is an Xsl-FO document. I tried the following: from xml.dom import minidom from xml.xpath import...
1
by: Greg Rothlander | last post by:
I posted this a few days ago and didn't get any response. I try again but ask it a little differently. I'm recieving any XML document from a client and I need to convert it to an ASCII delimited...
2
by: Dag | last post by:
Hi I am pretty much an xml beginner; hopefully someone can easily answer this one... I want to select a node representing a worksheet in an xml document for the Office Web Components spreadsheet....
2
by: Carlo Garcia | last post by:
Hi I am having trouble with namespaces: Using the .NET System.XML classes, whenever I run an xpath on a file with a namespace, the xpath query returns nothing, but when I remove the namespace...
19
by: David Thielen | last post by:
Hi; If there are no namespaces this works fine for me. But if the xml has namespaces, then I get either no node back or an exception. Here is the sample xml: <root xmlns="http://www.test.org"...
11
by: David Thielen | last post by:
Hi; Once I have loaded an xml file into a DOM, is there any way to then get any namespace declarations that were in the xml file? Or are those just discarded as the DOM is populated? And if...
6
by: AMDRIT | last post by:
Hello Everyone, I am having an issue with xml and namespaces, at least I think it is namespaces. When I use namespaces, I cannot use SelectSingleNode / SelectNodes as they always return...
2
by: matthewjbarr | last post by:
Sorry for cross-posting, but I posted this in the PHP group and think it probably should have been here - think maybe I should be looking at XPath expressions to solve the problem. E.g. something...
1
by: reyesvsn | last post by:
Hello everybody, I have a question concerning XPath expressions and namespaces. Consider this XML: <?xml version="1.0" encoding="UTF-8"?> <newsMessage ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.