[xsl] Counting Preceding Cousins (and only cousins)

Subject: [xsl] Counting Preceding Cousins (and only cousins)
From: "Jordan (Wraezor)" <wraezor@xxxxxxxxxxx>
Date: Sat, 08 Apr 2006 23:36:54 -0600
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