Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: xpath descendant question

From: Peter Flynn <peter.nosp@-.--------.-->
To: NULL
Date: 2/7/2009 3:16:00 PM
CAFxX wrote:
> I found just a minor error in your code, it should be
> 
> //*[contains(child::text(),'Y')]/ancestor::a[1]
> 
> and not
> 
> //*[contains(.,'Y')]/ancestor::a[1]
> 
> right?

In some circumstances...it depends on what other markup is in your 
real-life document. If you want to find all a elements which have a Y in 
any element within them, regardless of how deep, then my example is 
correct, because <a><b>Y</b></a> contains Y somewhere in it.But the same 
is then true of your outer a as well, because 
<a><b>X</b><a><c><b>Y</b></c></a></a> contains XY, which contains a Y.

If what you really want is the a ancestor of all b elements that 
directly contain Y, then you need to specify that:

//b[contains(text(),'Y')]/ancestor::a[1]

This will return your nested a element rather than both a elements.

///Peter
-- 
XML FAQ: http://xml.silmaril.ie/


> Peter Flynn ha scritto:
>> CAFxX wrote:
>>> Hi everybody, I have a problem with a xpath query.
>>> Suppose you have something like this:
>>>
>>> <a>
>>>   <b>X</b>
>>>   <a>
>>>     <c>
>>>       <b>Y</b>
>>>     </c>
>>>   </a>
>>> </a>
>>>
>>> Right now to obtain a list of <a> containg a certain token I do:
>>>
>>> //a[ contains( descendant-or-self::*, 'token' ) ]
>>>
>>> the problem is that, in the case above, searching for the token Y 
>>> would give both <a>s, whereas I need only the innermost one (i.e. the 
>>> nearest <a> ancestor of the fragment containing the searched token). 
>>> How can I translate this in xpath?
>>
>> //*[contains(.,'Y')]/ancestor::a[1]
>>
>> ///Peter


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent