473,909 Members | 6,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XPath and namespaces...

Hi,
I've got a little bit of a problem when dealing with namespaces and XPath.

I'm trying very basic things, like showing all the nodes of one particular
namespace. Here is my XPath statement:
//*[local-name() = 'buch' and namespace-uri() =
'http://www.example.com/buecher']

Unfortunately this doesn't work. I have found several ways to solve this
with XSLT, but I need the pure XPath statement.

My XML file looks like this:

<?xml version="1.0"?>
<!-- Dateiname: Sammlung.xml -->
<SAMMLUNG
xmlns:buch="htt p://www.example.com/buecher"
xmlns:cd="http://www.example.com/cds">

<buch:ARTIKEL Status="vorhand en">
<buch:TITEL>T he Adventures of Huckleberry Finn</buch:TITEL>
<buch:AUTOR>Mar k Twain</buch:AUTOR>
<buch:PREIS>12. 75</buch:PREIS>
</buch:ARTIKEL>
<cd:ARTIKEL>
<cd:TITEL>Violi nkonzert D-Dur</cd:TITEL>
<cd:KOMPONIST>B eethoven</cd:KOMPONIST>
<cd:PREIS>14.95 </cd:PREIS>
</cd:ARTIKEL>
</SAMMLUNG>
Could anyone please give me a hint how to solve this problem?

Thanks,
Stefan
Jul 20 '05 #1
6 2232


Stefan Franke wrote:

I've got a little bit of a problem when dealing with namespaces and XPath.

I'm trying very basic things, like showing all the nodes of one particular
namespace. Here is my XPath statement:
//*[local-name() = 'buch' and namespace-uri() =
'http://www.example.com/buecher']

Unfortunately this doesn't work.
What do you mean by "doesn't work"? Do you get an error? Which software
are you using to test that XPath expression?

My XML file looks like this:

<?xml version="1.0"?>
<!-- Dateiname: Sammlung.xml -->
<SAMMLUNG
xmlns:buch="htt p://www.example.com/buecher"
xmlns:cd="http://www.example.com/cds">

<buch:ARTIKEL Status="vorhand en">
<buch:TITEL>T he Adventures of Huckleberry Finn</buch:TITEL>
<buch:AUTOR>Mar k Twain</buch:AUTOR>
<buch:PREIS>12. 75</buch:PREIS>
</buch:ARTIKEL>
<cd:ARTIKEL>
<cd:TITEL>Violi nkonzert D-Dur</cd:TITEL>
<cd:KOMPONIST>B eethoven</cd:KOMPONIST>
<cd:PREIS>14.95 </cd:PREIS>
</cd:ARTIKEL>
</SAMMLUNG>


Well looking at your example XML the expression

//*[local-name() = 'buch' and namespace-uri() =
'http://www.example.com/buecher']

will not find any element as the XML doesn't contain any
<buch xmlns="http://www.example.com/buecher" />
elements, perhaps you are looking for

//*[local-name() = 'ARTIKEL' and namespace-uri() =
'http://www.example.com/buecher']
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2
> perhaps you are looking for

//*[local-name() = 'ARTIKEL' and namespace-uri() =
'http://www.example.com/buecher']


oops, indeed I am.

Thanks very much,
Stefan
Jul 20 '05 #3
Hi,
I'm still having troubles with XPath and namespaces.

The XML I'm using looks like this:

<SAMMLUNG
xmlns:buch="htt p://www.example.com/buecher"
xmlns:cd="http://www.example.com/cds">

<buch:ARTIKEL Status="vorhand en">
<buch:TITEL>T he Marble Faun</buch:TITEL>
<buch:AUTOR>Nat haniel Hawthorne</buch:AUTOR>
<buch:PREIS>10. 95</buch:PREIS>
</buch:ARTIKEL>
<cd:ARTIKEL>
<cd:TITEL>Violi nkonzerte 1, 2 und 3</cd:TITEL>
<cd:KOMPONIST>M ozart</cd:KOMPONIST>
<cd:PREIS>16.49 </cd:PREIS>
</cd:ARTIKEL>
</SAMMLUNG>
If I want to display e.g. all the CDs of Mozart, I would do something like
this if I wouldn't have to deal with namespaces:

string(//*/TITEL[following-sibling::KOMPON IST='Mozart'])

My suggestion when dealing with namespaces would be something like this (but
it doesn't work, as there is no TITEL-element anywhere, but I don't know how
to get it in there):

string(//*[local-name()='ARTIKEL ' and
namespace-uri()='http://www.example.com/cds']/*[string(local-name()='KOMPONI ST')
= 'Mozart'])

Anyway, as I said before, that doesn't work. Could someone please help me
with that?

Thanks,
Stefan
Jul 20 '05 #4


Stefan Franke wrote:

The XML I'm using looks like this:

<SAMMLUNG
xmlns:buch="htt p://www.example.com/buecher"
xmlns:cd="http://www.example.com/cds">

<buch:ARTIKEL Status="vorhand en">
<buch:TITEL>T he Marble Faun</buch:TITEL>
<buch:AUTOR>Nat haniel Hawthorne</buch:AUTOR>
<buch:PREIS>10. 95</buch:PREIS>
</buch:ARTIKEL>
<cd:ARTIKEL>
<cd:TITEL>Violi nkonzerte 1, 2 und 3</cd:TITEL>
<cd:KOMPONIST>M ozart</cd:KOMPONIST>
<cd:PREIS>16.49 </cd:PREIS>
</cd:ARTIKEL>
</SAMMLUNG>
If I want to display e.g. all the CDs of Mozart, I would do something like
this if I wouldn't have to deal with namespaces:

string(//*/TITEL[following-sibling::KOMPON IST='Mozart'])
Not really, if you apply the string function to a nodeset then you get
the string value of the first node in document order so the above
doesn't give you all CD titles but only one.
My suggestion when dealing with namespaces would be something like this (but
it doesn't work, as there is no TITEL-element anywhere, but I don't know how
to get it in there):

string(//*[local-name()='ARTIKEL ' and
namespace-uri()='http://www.example.com/cds']/*[string(local-name()='KOMPONI ST')
= 'Mozart'])


How are you evaluating your XPaths? You should simply be able to declare
prefixes for the namespaces and then use e.g.
//prefix:TITLE
instead of going to the pain of using namespace-uri comparisons.
So I think you should better tell us about the software you are using to
evaluate XPaths and then probably someone can tell you how to use
namespace prefixes bound to namespaces when evaluating XPath expressions.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #5
> How are you evaluating your XPaths? You should simply be able to declare
prefixes for the namespaces and then use e.g.
//prefix:TITLE

Hi Martin,
I'm using FiveSight's XPathTester (Version 1.4 for Saxon - 3 December 2001).
When I try something like "//*/cd:TITEL" an error message comes up and tells
me that 'Prefix cd has not been declared'. So I thought, oh well, I have to
find a way around this problem. Am I wrong in thinking that?

regards,
Stefan
Jul 20 '05 #6


Stefan Franke wrote:

I'm using FiveSight's XPathTester (Version 1.4 for Saxon - 3 December 2001).
When I try something like "//*/cd:TITEL" an error message comes up and tells
me that 'Prefix cd has not been declared'. So I thought, oh well, I have to
find a way around this problem. Am I wrong in thinking that?


No, you need to bind that prefix to the proper namespace but an XPath
tool should give you a way to do that before evaluating an expression.
I don't know FiveSight's XPathTester and it seems it is no longer
available on their web site.
Saxon 6.5 as documented here
<http://saxon.sourcefor ge.net/saxon6.5.3/api-guide.html#Expr essions>
has a method
declareNamespac e
in its XPath API so with Java and Saxon it should be possible to bind
prfixes to namespaces before evaluating an expression.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #7

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

Similar topics

2
2056
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 Evaluate from xml import sax parser = sax.make_parser() parser.setFeature(sax.handler.feature_namespaces,1) parser.setFeature(sax.handler.feature_namespace_prefixes,1) doc = minidom.parse("fo-file.xml",parser)
1
1165
by: David Williams | last post by:
Is there an existing method that will return the XPath that would give the current node? In other words, using the following XML document: <root> <node value="1"/> <node value="2"/> <--- assume is current node <node value="3"/> </root> I want something that would return "//root/node". From either
19
16250
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" xmlns:sns="http://www.test.org/sub" xmlns:mns="http://www.test.org/mini"> <data>
6
15519
by: Chua Wen Ching | last post by:
Hi there, I had this xml file with me (not yet consider implementing xml namespaces yet). <?xml version='1.0'?> <Object> <Windows> <EID>1</EID> <EDesc>Error 1</EDesc> </Windows>
3
6809
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer> </Root> I would like to retreive "\Root\Customer\Name" out of it. Something like:
6
3019
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 0/Nothing respectively. Anyone have any thoughts? Thanks in advance VB.Net 2005
14
2018
by: Mat| | last post by:
Hello :-) I am learning XPath, and I am trying to get child nodes of a node whose names do *not* match a given string, e.g : <dummy> <example> <title>Example 1</title> <body>this is an example</body>
6
7302
by: J.Marsch | last post by:
I must be completely losing my mind. I have some code that writes to config files. It works great with app.config files, but fails miserably with web.config files. For the life of me, I cannot figure out what is going on here. I have taken it all the way back to just selecting the configuration node (top level node), and it fails! How can this line fail (returns null)??
14
17340
by: Mikhail Teterin | last post by:
Hello! What's would be the syntax for a query, which would allow me to get only the elements with non-empty text-nodes? For example, from: <a><b></b></a> <c/> <d><e>meow</e></d>
0
9877
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
11346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11046
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
10538
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...
1
8097
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
7248
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();...
0
5938
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3357
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.