// inside a predicate behaves like /descendant:: ???

Questions about XML that are not covered by the other forums should go here.
schmidlinF
Posts: 1
Joined: Wed Jun 17, 2009 12:45 pm

// inside a predicate behaves like /descendant:: ???

Post by schmidlinF »

I this a bug or a feature of xpath?

the following xpath:

Code: Select all

/A/B[//C='want']
on the following XML:

Code: Select all

<A>
<B><C>don't want</C></B>
<B><C>want</C></B>
<B><C>don't want</C></B>
</A>
returns 3 nodes instead of just the one.

to get the result I want I need to use

Code: Select all

 /A/B[descendant::C='want']
I though

Code: Select all

//
was equivalent to

Code: Select all

descendant:: 
but inside a predicate it seems to be equivalent to
/descendant::
, i.e. select descendant nodes of the root element??

Have I missed something?

PS: example kept deliberately simple: please do not tell me I do not need // to get what I want :-)
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: // inside a predicate behaves like /descendant:: ???

Post by sorin_ristache »

Hello,
schmidlinF wrote:I though

Code: Select all

//
was equivalent to

Code: Select all

descendant:: 
but inside a predicate it seems to be equivalent to
/descendant::
, i.e. select descendant nodes of the root element??

Have I missed something?
It is equivalent to /descendant-or-self::node()/ as you can read in the XPath specification:
// is short for /descendant-or-self::node()/. For example, //para is short for /descendant-or-self::node()/child::para and so will select any para element in the document

Regards,
Sorin
Post Reply