Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XPath and namespaces...

Reply
Thread Tools

XPath and namespaces...

 
 
Stefan Franke
Guest
Posts: n/a
 
      01-02-2005
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>
<buchREIS>12.75</buchREIS>
</buch:ARTIKEL>
<cd:ARTIKEL>
<cd:TITEL>Violinkonzert D-Dur</cd:TITEL>
<cd:KOMPONIST>Beethoven</cd:KOMPONIST>
<cdREIS>14.95</cdREIS>
</cd:ARTIKEL>
</SAMMLUNG>


Could anyone please give me a hint how to solve this problem?

Thanks,
Stefan


 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-02-2005


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>
> <buchREIS>12.75</buchREIS>
> </buch:ARTIKEL>
> <cd:ARTIKEL>
> <cd:TITEL>Violinkonzert D-Dur</cd:TITEL>
> <cd:KOMPONIST>Beethoven</cd:KOMPONIST>
> <cdREIS>14.95</cdREIS>
> </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/
 
Reply With Quote
 
 
 
 
Stefan Franke
Guest
Posts: n/a
 
      01-02-2005
> perhaps you are looking for
>
> //*[local-name() = 'ARTIKEL' and namespace-uri() =
> 'http://www.example.com/buecher']
>


oops, indeed I am.

Thanks very much,
Stefan


 
Reply With Quote
 
Stefan Franke
Guest
Posts: n/a
 
      01-03-2005
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>
<buchREIS>10.95</buchREIS>
</buch:ARTIKEL>
<cd:ARTIKEL>
<cd:TITEL>Violinkonzerte 1, 2 und 3</cd:TITEL>
<cd:KOMPONIST>Mozart</cd:KOMPONIST>
<cdREIS>16.49</cdREIS>
</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


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      01-03-2005


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>
> <buchREIS>10.95</buchREIS>
> </buch:ARTIKEL>
> <cd:ARTIKEL>
> <cd:TITEL>Violinkonzerte 1, 2 und 3</cd:TITEL>
> <cd:KOMPONIST>Mozart</cd:KOMPONIST>
> <cdREIS>16.49</cdREIS>
> </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/
 
Reply With Quote
 
Stefan Franke
Guest
Posts: n/a
 
      01-03-2005
> 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


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      01-03-2005


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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
XPath: efficiency in xpath expressions Tjerk Wolterink XML 1 11-13-2004 06:03 PM
Are there any XPath parsers that generate XPath trees? goog XML 0 01-14-2004 01:47 PM
XPath that does not include other XPath Anna XML 0 07-31-2003 07:55 AM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments