XPath abbreviated form

N

nils.grimsmo

What is the the syntax of the abbreviated form of the following XPath
query?

child::a/descendant::b[descendant::text()="foo"]

I try with

a//b[//text()="foo"]

but I get two nodes selected instead of one in

<?xml version="1.0"?>
<a>
<x>
<b>
<u>foo</u>
<v>bar</v>
</b>
</x>
<y>
<b>
<w>bar</w>
</b>
</y>
</a>

I am using Jaxen 1.1.1.


Klem fra Nils
 
P

pgfearo

What is the the syntax of the abbreviated form of the following XPath
query?

child::a/descendant::b[descendant::text()="foo"]

I try with

a//b[//text()="foo"]

but I get two nodes selected instead of one in

<?xml version="1.0"?>
<a>
<x>
<b>
<u>foo</u>
<v>bar</v>
</b>
</x>
<y>
<b>
<w>bar</w>
</b>
</y>
</a>

I am using Jaxen 1.1.1.

Klem fra Nils

The expression below should work and return only one node

a//b[.//text() = "foo"]

This provides only one node by prefixing '//' with '.' (self) in the
predicate.



Phil Fearon
http://www.sketchpath.com
 
R

Richard Tobin

What is the the syntax of the abbreviated form of the following XPath
query?

child::a/descendant::b[descendant::text()="foo"]

I try with

a//b[//text()="foo"]

the "//text()" in the predicate means and text descendants of the root
node. To find descendants of the context node, you need ".//text()".

Incidentally, "//" isn't exactly the same as "descendant". In
particular, "//*[1]" is not the same as "/descendant::*[1]", but it
doesn't make any difference in this case.

-- Richard
 
P

Philippe Poulard

(e-mail address removed) a écrit :
What is the the syntax of the abbreviated form of the following XPath
query?

child::a/descendant::b[descendant::text()="foo"]

I try with

a//b[//text()="foo"]

but I get two nodes selected instead of one in

// is short for /descendant-or-self::node()/

a//b[//text()="foo"]
means
a//b[/descendant-or-self::node()/="foo"]
it starts from the root

a//b[.//text()="foo"]
means
a//b[./descendant-or-self::node()/="foo"]
it doesn't start from the root
 
J

Justin Johansson

Me thinks this:

a//b[.//text()='foo']

What appears to be wrong with
a//b[//text()="foo"]
is that you are selecting descendants of the entire document root in the
predicate //text() whereas you really only want descendants of b
elements. Therefore you need to put a dot before the slashslash in the
predicate.

Cheers
Justin Johansson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top