RE: [xsl] Count of previous nodes until a child node of a previous node is found with certain attribute value

Subject: RE: [xsl] Count of previous nodes until a child node of a previous node is found with certain attribute value
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sat, 26 May 2007 21:48:25 +0100
David's solution is probably the best that can be done in 1.0.

I've often felt the need for a higher-order "until" operator:

$sequence until condition

which selects all the nodes in $sequence up to [perhaps including] the first
one where condition is true.

In 2.0, you can do this as:

subsequence($sequence, 1, index-of($sequence/condition, true())[1]])

or in your case, since you only want the number of elements

index-of(preceding-sibling::p/(component/@name='newpage'), true())[1]

This has a better chance than David's solution of evaluating the
preceding-sibling axis only as far as the first qualifying node.


Michael Kay
http://www.saxonica.com/  


> -----Original Message-----
> From: Jeremy Freedman [mailto:jeremy.freedman@xxxxxxxxx] 
> Sent: 26 May 2007 20:22
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Count of previous nodes until a child node of 
> a previous node is found with certain attribute value
> 
> Hi all,
> 
> I'm the first to admit I'm not XSL expert, but I've been 
> stuck on this one for hours and I'm hoping someone can help me out.
> 
> I've found similar posts where a user is looking for a count 
> of following nodes until an attribute is found, but not close 
> enough to what I'm looking to do.
> 
> Assuming we have this XML structure:
> 
> <body>
>     <p>
>           text
>     </p>
>    <p>
>           text2
>     </p>
>     <p>
>           <component name="newpage" />
>     </p>
>     <p>
>           text3
>     </p>
>    <p>
>           text4
>     </p>
>     <p>
>           text5
>     </p>
>    <p>
>           text6
>     </p>
>     <p>
>           <component name="newpage" />
>     </p>
>     <p>
>           text7
>     </p>
>    <p>
>           text8
>     </p>
>     <p>
>           text9
>     </p>
> </body>
> 
> 
> I want to be able to find the count of preceding "p" nodes 
> from the p node I am on, up until I find a "p" node with a 
> child node named "component" with attribute name=newpage.
> 
> Examples:
> 
> If I am on the p node whose value is "text8", I should get a count of
> 1 because there is only 1 previous p node before I find a p 
> node with the component node with attribute name=newpage.
> 
> p node value            Count
> text                          0
> text2                        1
> text3                        0
> text4                        1
> text5                        2
> text6                        3
> text7                        0
> text8                        1
> text9                        2
> 
> 
> You will be my personal hero is you can help me out!
> 
> Much thanks,
> 
> Jeremy

Current Thread