Re: [xsl] preceding with the same ancestor as self

Subject: Re: [xsl] preceding with the same ancestor as self
From: "Rick Quatro rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 27 Feb 2015 21:57:58 -0000
Thanks Michael. I am (finally) using XPath 2.0 so I can use the "is"
operator. Thank for the detailed explanation.

Rick

-----Original Message-----
From: Michael Kay mike@xxxxxxxxxxxx
[mailto:xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx] 
Sent: Friday, February 27, 2015 4:15 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] preceding with the same ancestor as self



> I think I have it:
> 
> preceding::fig[.!="" and ancestor::grp = current()/ancestor::grp][1]

It's a bad mistake to use "=" to test whether two nodes are the same node
(so to speak). You're actually comparing the string values of the nodes,
which is a very expensive operation for nodes that have large subtrees, and
if you're unlucky it could give you a false positive. In 2.0, use the "is"
operator. In 1.0, compare node identity using generate-id(), or
count($X|$Y)=1.

The other problem with this expression is that if there isn't a preceding
figure within the subtree, it could take a long search to find out (back to
the start of the document).

It might be worth noting that preceding::x gives the same nodes as
ancestor-or-self::*/preceding-sibling::*/descendant-or-self::x. Using this
equivalence, you can stop the search earlier by qualifying the first step:

ancestor-or-self::*[ancestor::grp]/preceding-sibling::*/descendant-or-self::
x

Michael Kay
Saxonica

> 
> -----Original Message-----
> From: Rick Quatro rick@xxxxxxxxxxxxxx
> [mailto:xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx]
> Sent: Friday, February 27, 2015 3:44 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] preceding with the same ancestor as self
> 
> Hi,
> 
> When I am at a <fig> with no text, I want to find the previous <fig> 
> that has text, but only within the same ancestor group.
> 
> I am using this:
> 
> preceding::fig[.!=""][1]
> 
> This works for all of the empty <fig> elements in the first <grp>, but 
> in the second <grp>, it incorrectly picks up the <fig> with the 4 
> value from the previous <grp>. How can I restrict the preceding axis 
> so a found node will have the same ancestor as the context node? Thanks.
> 
> Rick
> 
> 
> <?xml version="1.0" encoding="UTF-8"?> <doc>
>    <grp>
>        <fig/>
>        <fig>1</fig>
>        <fig>2</fig>
>        <fig/>
>        <sub>
>            <fig>3</fig>
>        </sub>
>        <fig/>
>        <fig>4</fig>
>    </grp>
>    <grp>
>        <fig/>
>        <fig>1</fig>
>        <fig>2</fig>
>        <fig>3</fig>
>        <sub>
>            <fig/>
>        </sub>
>        <fig/>
>    </grp>
> </doc>

Current Thread