xsl-list
[Top] [All Lists]

Recursion of variable, pt. 2

2006-01-10 09:28:01
I'm stumped!  I need help with variable recursion based on grouping of 
elements.  Thanks for your help...

The specifics:
Hierarchy: Nested <r1>,<r2>,<r3>...
Desired output: (see also below) <cnt> Starting with 1 per grouping of <q>, 
adds <cnt> amount to the preceding value of <cnt> per grouping of <q>.  <cnt> 
is always at the lowest <r?> level.  I have a stylesheet that will increment 
<cnt> nicely, but I want it to start over with "1" when <q> changes.

Sample code:

<?xml version="1.0"?>
<vector>
<r1>
<t>geometry</t>
<r2>
<t>parabola</t>
<r3>
<t>trapezium</t>
<q>1</q>
<cnt>4</cnt>
</r3>
<r3>
<t>rhombus</t>
<q>1</q>
<cnt>4</cnt>
</r3>
</r2>
<r2>
<t>parallelogram</t>
<q>1</q>
<cnt>3</cnt>
</r2>
</r1>
<r1>
<t>reflection</t>
<q>2</q>
<cnt>2</cnt>
</r1>
<r1>
<t>refraction</t>
<q>2</q>
<cnt>2</cnt>
</r1>
</vector>

Current stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml"/>

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="q"/>
<xsl:template match="cnt">
<xsl:variable name="cs" select="preceding::cnt"/>
<xsl:variable name="c" select="sum($cs)"/>
<q><xsl:value-of select="preceding-sibling::q"/></q>
<o>
<xsl:choose>
<xsl:when test="$cs = 1 or position() != 1">
<xsl:value-of select="$c + 1"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$c"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test=". &gt; 1">-<xsl:value-of select="$c + ."/></xsl:if>
</o>
</xsl:template>
</xsl:stylesheet>

Desired output:

<vector>
<r1>
<t>geometry</t>
<r2>
<t>parabola</t>
<r3>
<t>trapezium</t>
<q>1</q>
<o>1-5</o>
</r3>
<r3>
<t>rhombus</t>
<q>1</q>
<o>6-10</o>
</r3>
</r2>
<r2>
<t>parallelogram</t>
<q>1</q>
<o>11-14</o>
</r2>
</r1>
<r1>
<t>reflection</t>
<q>2</q>
<o>1-2</o>
</r1>
<r1>
<t>refraction</t>
<q>2</q>
<o>3-4</o>
</r1>
</vector>


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>
  • Recursion of variable, pt. 2, watchstone(_at_)netzero(_dot_)com <=