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

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="http://www.example.com/buecher"
xmlns:cd="http://www.example.com/cds">

<buch:ARTIKEL Status="vorhanden">
<buch:TITEL>The Adventures of Huckleberry Finn</buch:TITEL>
<buch:AUTOR>Mark Twain</buch:AUTOR>
<buch:PREIS>12.75</buch:PREIS>
</buch:ARTIKEL>
<cd:ARTIKEL>
<cd:TITEL>Violinkonzert D-Dur</cd:TITEL>
<cd:KOMPONIST>Beethoven</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 2206


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="http://www.example.com/buecher"
xmlns:cd="http://www.example.com/cds">

<buch:ARTIKEL Status="vorhanden">
<buch:TITEL>The Adventures of Huckleberry Finn</buch:TITEL>
<buch:AUTOR>Mark Twain</buch:AUTOR>
<buch:PREIS>12.75</buch:PREIS>
</buch:ARTIKEL>
<cd:ARTIKEL>
<cd:TITEL>Violinkonzert D-Dur</cd:TITEL>
<cd:KOMPONIST>Beethoven</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="http://www.example.com/buecher"
xmlns:cd="http://www.example.com/cds">

<buch:ARTIKEL Status="vorhanden">
<buch:TITEL>The Marble Faun</buch:TITEL>
<buch:AUTOR>Nathaniel Hawthorne</buch:AUTOR>
<buch:PREIS>10.95</buch:PREIS>
</buch:ARTIKEL>
<cd:ARTIKEL>
<cd:TITEL>Violinkonzerte 1, 2 und 3</cd:TITEL>
<cd:KOMPONIST>Mozart</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::KOMPONIST='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()='KOMPONIST')
= '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="http://www.example.com/buecher"
xmlns:cd="http://www.example.com/cds">

<buch:ARTIKEL Status="vorhanden">
<buch:TITEL>The Marble Faun</buch:TITEL>
<buch:AUTOR>Nathaniel Hawthorne</buch:AUTOR>
<buch:PREIS>10.95</buch:PREIS>
</buch:ARTIKEL>
<cd:ARTIKEL>
<cd:TITEL>Violinkonzerte 1, 2 und 3</cd:TITEL>
<cd:KOMPONIST>Mozart</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::KOMPONIST='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()='KOMPONIST')
= '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.sourceforge.net/saxon6.5.3/api-guide.html#Expressions>
has a method
declareNamespace
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
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: 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"/> <---...
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"...
6
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
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>...
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...
14
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...
6
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...
14
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
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.