RE: How to Reorder Items & Number Them Sequentially?

Subject: RE: How to Reorder Items & Number Them Sequentially?
From: "Jarno Elovirta" <jarno@xxxxxxxxxxxxxx>
Date: Mon, 22 May 2000 16:33:29 +0300
this does it, but i'm quite sure there are better ways of doing it.

[c:\temp]type test.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<animals>
  <animal type="dog" name="Fido" />
  <animal type="cat" name="Kitty" />
  <animal type="bird" name="Tweety" />
  <animal type="horse" name="Trigger" />
  <animal type="cat" name="Tom" />
  <animal type="pig" name="Porky" />
  <animal type="fish" name="Charlie" />
  <animal type="pig" name="Babe" />
  <animal type="cow" name="Elsie" />
  <animal type="cat" name="Puss" />
</animals>

[c:\temp]type test.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="text" />

<xsl:key name="sort" match="animal" use="@type"/>
<xsl:variable name="list" select="animals/animal[generate-id(.) =
generate-id(key('sort', @type))]" />

<xsl:template match="/">
  <xsl:call-template name="process-list">
    <xsl:with-param name="index" select="1" />
    <xsl:with-param name="position" select="1" />
    <xsl:with-param name="counter" select="0" />
  </xsl:call-template>
</xsl:template>

<xsl:template name="process-list">
  <xsl:param name="index" />
  <xsl:param name="position" />
  <xsl:param name="counter" />
  <xsl:choose>
    <!-- process new @type -->
    <xsl:when test="$counter = count($list[@type = $list[$index]/@type]) -
1">
      <xsl:apply-templates select="$list[$index]">
        <xsl:with-param name="position" select="$position" />
      </xsl:apply-templates>
      <xsl:call-template name="process-list">
        <xsl:with-param name="index" select="$index" />
        <xsl:with-param name="position" select="$position + 1" />
        <xsl:with-param name="counter" select="1" />
      </xsl:call-template>
    </xsl:when>
    <!-- process the rest of the current @type -->
    <xsl:otherwise>
      <xsl:apply-templates select="animals/animal[@type =
$list[$index]/@type][$counter + 1]">
        <xsl:with-param name="position" select="$position" />
      </xsl:apply-templates>
      <xsl:choose>
        <!-- goto next @type -->
        <xsl:when test="$counter = count(animals/animal[@type =
$list[$index]/@type])">
          <!-- test if there are more @types -->
          <xsl:if test="$index &lt;= count($list)">
            <xsl:call-template name="process-list">
              <xsl:with-param name="index" select="$index + 1" />
              <xsl:with-param name="position" select="$position" />
              <xsl:with-param name="counter" select="0" />
            </xsl:call-template>
          </xsl:if>
        </xsl:when>
        <!-- goto next in this @type -->
        <xsl:otherwise>
          <xsl:call-template name="process-list">
            <xsl:with-param name="index" select="$index" />
            <xsl:with-param name="position" select="$position + 1" />
            <xsl:with-param name="counter" select="$counter + 1" />
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="animal">
  <xsl:param name="position" />
  <xsl:value-of select="$position" />
  <xsl:text>.  </xsl:text>
  <xsl:value-of select="@type" />
  <xsl:text> (</xsl:text>
  <xsl:value-of select="@name" />
  <xsl:text>)&#xA;</xsl:text>
</xsl:template>

</xsl:stylesheet>

[c:\temp]saxon test.xml test.xsl
1.  dog (Fido)
2.  cat (Kitty)
3.  cat (Tom)
4.  cat (Puss)
5.  bird (Tweety)
6.  horse (Trigger)
7.  pig (Porky)
8.  pig (Babe)
9.  fish (Charlie)
10.  cow (Elsie)

, which is what i understand you were looking for...?

--
Jarno Elovirta     jarno.elovirta@xxxxxxxxxxxxxx
CODEONLINE Ltd.    http://www.codeonline.com
P.O. Box 538 (Ukonvaaja 2 A), FIN-02130 Espoo, Finland
Mobile: +358 40 747 5572 Fax: +358 9 4393 0410

"Hoc non credo; toga mea surrepta est iterum!"


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread