Re: [xsl] XSLT recursion problem

Subject: Re: [xsl] XSLT recursion problem
From: Ragulf Pickaxe <ragulf.pickaxe@xxxxxxxxx>
Date: Fri, 23 Sep 2005 15:14:41 +0200
On 9/23/05, Paolo Vaccari <vaccari@xxxxxxxxxxxx> wrote:
> 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.
<snip/>
> 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?
> Thanks
> Paolo Vaccari


Hi Paolo,

I don't know what you want with the sometag element, so it is a little
hard answering that part. What I would do, though, is to move the
processing down to the elements that you are working with (so you have
better control over when to output "sometag").

The solution should also enable you to make more types of elements
(besides txt and img) without modification.

I don't know if this will meet your requirements.
It is untested, though

<xsl:template match="ax:repeat">
  <xsl:apply-templates select="sometag"/>
</xsl:template>

<xsl:template match="sometag">
  <xsl:apply-templates select="ax:items[1]" mode="first"/>
</xsl:template>

<xsl:template match="ax:items" mode="first">
  <xsl:param name="pos" select="'1'"/>

  <sometag>
    <xsl:element name="{@element}">
      <xsl:attribute name="src">
        <xsl:value-of select="ax:item[$pos]/@src"/>
      </xsl:attribute>
    </xsl:element>
    <xsl:apply-templates select="following-sibling::ax:items" mode="further">
      <xsl:with-param name="pos" select="$pos"/>
    </xsl:apply-templates>
  </sometag>

  <xsl:if test="ax:item[$pos+1]"> <!-- More items? -->
    <xsl:apply-templates select="ax:items[1]" mode="first">
      <xsl:with-param name="pos" select="$pos+1"/>
    </xsl:apply-templates>
  </xsl:if>
</xsl:template>

<xsl:template match="ax:items" mode="further">
  <xsl:param name="pos"/>
  <xsl:element name="{@element}">
    <xsl:attribute name="src">
      <xsl:value-of select="ax:item[$pos]/@src"/>
    </xsl:attribute>
  </xsl:element>
</xsl:template>

I hope this helps

Regards,
Ragulf Pickaxe :-)

Current Thread