[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Counting Preceding Cousins (and only cousins)


Subject: RE: [xsl] Counting Preceding Cousins (and only cousins)
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sun, 9 Apr 2006 16:33:54 +0100

Try:

count(preceding-sibling::p) + count(../preceding-sibling::block/p)

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

> -----Original Message-----
> From: Jordan (Wraezor) [mailto:wraezor@xxxxxxxxxxx] 
> Sent: 09 April 2006 06:37
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Counting Preceding Cousins (and only cousins)
> 
> Hello,
> 
> I'm trying to figure out how to count the preceding set of 
> cousins of a  
> particular element, but am having trouble figuring out the 
> proper method.
> 
> My XML goes something like so:
> 
> <div>
>    <p></p>
>    <block>
>      <p></p>
>      <p></p>
>    </block>
>    <p></p>
>    <p></p>
>    <block>
>      <p></p>
>    </block>
> </div>
> 
> I'm trying to count all the preceding /block/p elements of 
> the current  
> one, being sure to not count 'p' elements at different levels, etc.
> 
> My XSL looks something like this:
> 
> <xsl:template match="block" mode="main">
>    <xsl:apply-templates mode="grandchild" />
> </xsl:template>
> 
> <xsl:template match="p" mode="grandchild">
>    <xsl:number format="1." level="multiple" 
> value="count(preceding::p)  
> + 1"/>
> </xsl:template>
> 
> Unfortunately, the above counts all <p> elements in the whole 
> tree, not  
> just cousins (but uncles, aunts, and even the neighbour's 
> dog).  I also  
> tried to make something work with preceding-siblings, but 
> that obviously  
> only counts according to the current parent 'block', so the 
> numbering  
> restarts for each new parent.
> 
> I even got a count of all immediate cousins, however that was 
> just a total  
> count, not relative to the location of the current node (i.e. 
> preceding).   
> In that case, I considered incorporating 'position()', but 
> couldn't figure  
> out how to get that to accurately know where it was.
> 
> I hunted around online for a while, but could only find examples of  
> slightly different problems.
> 
> What's the best way of solving this problem?
> 
> Thanks in advance for any assistance,
> Jordan


Current Thread
Keywords