[xsl] XSLT recursion problem

Subject: [xsl] XSLT recursion problem
From: Paolo Vaccari <vaccari@xxxxxxxxxxxx>
Date: Fri, 23 Sep 2005 13:44:42 +0200
Hi all,
I'm trying to write an extension for an xml language that doesn't have 
a "loop" or "repeat" statement. Thus I have to write some XSLT code 
to expand my own <repeat> element.
At the moment my best solution is the following.

For XML:
<ax:repeat xmlns:ax="...">
    <ax:items element="txt">
      <ax:item src="txt1.txt"/>
      <ax:item src="txt2.txt"/>
    <ax:items element="img">
      <ax:item src="img1.jpg"/>
      <ax:item src="img2.jpg"/>

The output I want is:
  <txt src="txt1.txt"/>
  <img src="img1.jpg"/>
  <txt src="txt2.txt"/>
  <img src="img2.jpg"/>

The XSLT to get this output is:
<xsl:stylesheet version="1.0" 

  <!-- apply templates for ax:repeat -->
  <xsl:template match="ax:repeat">
    <!-- the <item> to be processed at each loop-->
    <xsl:param name="index" select="1"/>
    <!-- process <items> and <sometag>, passing $index -->
      <xsl:with-param name="index" select="$index"/>
    <!-- check if other loops are needed -->
    <xsl:if test="$index &lt; 
      <!-- continue recursively -->
      <xsl:apply-templates select="self::ax:repeat">
        <xsl:with-param name="index" select="$index + 1"/>

  <!-- apply templates for ax:items -->
  <xsl:template match="ax:items">
    <xsl:param name="index"/>
    <xsl:element name="{@element}">
      <!-- element content -->

Ok, I have what I want, so what's the problem?
To get this, I have to propagate the $index parameter through the 
<sometag>, this way:

<xsl:template match="sometag">
  <xsl:param name="index" select="1"/>
      <xsl:with-param name="index" select="$index"/>
    <!-- sometag content -->

This is very bad, because I have to modify the <sometag> template, 
which has nothing to do with my extension.
Anyone has a better idea?
Paolo Vaccari

Current Thread