Re: [xsl] changing source-tree

Subject: Re: [xsl] changing source-tree
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 10 Nov 2005 14:27:13 +0000
Hi Manuel,

> I have the following problem:
> <foo>
>      <example_bar>
>      ...
>      </example_bar>
> </foo>
>
> foo triggers a template which splits a string into one or more
> pieces. example_bar is the element which describes on how to output
> one of them. Though I only know at runtime how many pieces there
> are.

I'd pass the <example_bar> element as a parameter to the template that
splits the string into pieces, and then, within that template, apply
templates to the <example_bar> element with the substring as the
argument.

Something like:

<xsl:template match="foo">
  <xsl:call-template name="split-string">
    <xsl:with-param name="string" select="..." />
    <xsl:with-param name="output-template" select="*" />
  </xsl:call-template>
</xsl:template>

<xsl:template name="split-string">
  <xsl:param name="string" />
  <xsl:param name="output-template" />
  <xsl:choose>
    <xsl:when test="contains($string, ' ')">
      <!-- deal with the substring based on the output template -->
      <xsl:apply-templates select="$output-template">
        <xsl:with-param name="string"
                        select="substring-before($string, ' ')" />
      </xsl:apply-templates>
      
      <!-- recurse through the rest of the string -->
      <xsl:call-template name="split-string">
        <xsl:with-param name="string"
                        select="substring-after($string, ' ')" />
        <xsl:with-param name="output-template"
                        select="$output-template" />
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="$string">
      <!-- deal with the string based on the output template -->
      <xsl:apply-templates select="$output-template">
        <xsl:with-param name="string" select="$string" />
      </xsl:apply-templates>
    </xsl:when>
  </xsl:choose>
</xsl:template>

<xsl:template match="foo/*">
  <xsl:param name="string" />
  <xsl:copy>
    <xsl:value-of select="$string" />
  </xsl:copy>
</xsl:template>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Current Thread