Re: [xsl] Iterating over values in a table of data

Subject: Re: [xsl] Iterating over values in a table of data
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 07 Jun 2006 11:42:25 -0400
Hi,

Backwards from Andrew's answer:

<input>
    <!-- This is a table of lookup values. -->
    <data>
        <item>
            <value>one</value>
        </item>
        <item>
            <value>two</value>
        </item>
        <item>
            <value>three</value>
        </item>
    </data>
    <!-- This node needs to be repeatedly transformed for each input
data item -->
    <repeat>
            Item <value-of name="value"/> is in position <position-of
name="value"/>
    </repeat>
</input>

<xsl:stylesheet ...>


<xsl:variable name="repeater" select="/input/repeat"/>

<xsl:template match="repeat"/>
<!-- suppresses output on default traversal -->

<xsl:template match="data">
  <xsl:for-each select="item">
    <xsl:apply-templates select="$repeater" mode="repeat">
      <xsl:with-param name="item" select="."/>
      <xsl:with-param name="pos" select="position()"/>
    </xsl:apply-templates>
  </xsl:for-each>
</xsl:template>

<xsl:template match="repeat" mode="repeat">
  <xsl:param name="item" select="/.."/>
  <xsl:param name="pos" select="0"/>
  <item>
    <xsl:apply-templates>
      <xsl:with-param name="item" select="$item"/>
      <xsl:with-param name="pos" select="position()"/>
    </xsl:apply-templates/>
  </item>
</xsl:template>

<xsl:template match="value-of">
  <xsl:param name="item"/>
  <xsl:value-of select="$item"/>
</xsl:template>

<xsl:template match="position-of">
  <xsl:param name="pos"/>
  <xsl:value-of select="$pos"/>
</xsl:template>

</xsl:stylesheet>

Untested.

Cheers,
Wendell

Current Thread