[xsl] Count previous-siblings w/same attribute value up to attribute value - 1 possible?

Subject: [xsl] Count previous-siblings w/same attribute value up to attribute value - 1 possible?
From: "Murray McDonald" <m.mcdonald@xxxxxxxxx>
Date: Mon, 12 Dec 2011 22:11:04 -0500
I would think Ken's grouping solution is the way to go - just to demonstrate
there is usually a number of ways one can solve a problem using XSLT ...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  xmlns:fn="http://www.w3.org/2005/xpath-functions";>
	
<xsl:output method="text"/>


	
<xsl:template match="item">
	<xsl:variable name="thisLevel" select="@level"/>
	<xsl:variable name="prevLevel"
select="string(number($thisLevel)-1)"/>
	<xsl:variable name="closest_prev_level"
select="preceding-sibling::item[@level eq $prevLevel][1]" as="node()*"/>
	<xsl:variable name="formats" select="('I.', 'A.', '1.')"
as="xs:string*"/>
	<xsl:variable name="format_for_this_level"
select="$formats[number($thisLevel)]"/>

    <xsl:variable name="designator" as="xs:string">
		<xsl:number count="item[@level eq $thisLevel]" from="item[.
is $closest_prev_level]" level="any" format="{$format_for_this_level}"/>
    </xsl:variable>

	
	<xsl:text>Item with numDisplay attribute &quot;</xsl:text>
	<xsl:apply-templates select="@numDisplay"/>
	<xsl:text>&quot;, designator is </xsl:text>
	<xsl:value-of select="$designator"/>
	<xsl:text>&#10;</xsl:text>
</xsl:template>	
</xsl:stylesheet>


Item with numDisplay attribute "I.", designator is I.
Item with numDisplay attribute "II.", designator is II.
Item with numDisplay attribute "A.", designator is A.
Item with numDisplay attribute "B.", designator is B.
Item with numDisplay attribute "C.", designator is C.
Item with numDisplay attribute "III.", designator is III.
Item with numDisplay attribute "A.", designator is A.
Item with numDisplay attribute "1.", designator is 1.
Item with numDisplay attribute "2.", designator is 2.
Item with numDisplay attribute "3.", designator is 3.
Item with numDisplay attribute "4.", designator is 4.
Item with numDisplay attribute "B.", designator is B.
Item with numDisplay attribute "1.", designator is 1.
Item with numDisplay attribute "2.", designator is 2.
Item with numDisplay attribute "3.", designator is 3.
Item with numDisplay attribute "C.", designator is C.

Current Thread