Re: xsl:for-each within an element?

Subject: Re: xsl:for-each within an element?
From: "Dan Machak" <machak@xxxxxxxxxxxx>
Date: Fri, 27 Aug 1999 10:12:17 -0700
On Aug 27,  9:00am, Rich Gazan wrote:
> Subject: xsl:for-each within an element?
> I have a whole mess of semicolon-delimited input that looks like this:
>
> <field>thing1; thing2;...thingn</field>
>
> I'm trying to transform it to this:
>
> <field>thing1</field>
> <field>thing2</field>
> etc...

I had the same problem with parsing a list of indeterminate length, and the
solution was to use a call-template in a recursive fashion. Here is a
stylesheet that should work (I ran this with XT19990813)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";>

<xsl:template match="/">
  <result>
  <xsl:call-template name="recurse">
    <xsl:with-param name="parse-string">
      thing1; thing2; thing3; thing4
    </xsl:with-param>
  </xsl:call-template>
  </result>
</xsl:template>

<xsl:template name="recurse">
  <xsl:param name="parse-string"></xsl:param>
  <xsl:if test="not($parse-string='')">
    <field>
      <xsl:choose>
      <xsl:when test="contains($parse-string, ';')">
        <xsl:value-of select="substring-before($parse-string, ';')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$parse-string"/>
      </xsl:otherwise>
      </xsl:choose>
    </field>
    <xsl:call-template name="recurse">
      <xsl:with-param name="parse-string">
        <xsl:value-of select="substring-after($parse-string, ';')"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>

>
> Rich Gazan
>

>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>-- End of excerpt from Rich Gazan

Dan Machak


-- 
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
<> Dan Machak                    <>  machak@xxxxxxxxxxxx  <>
<> MS T27A-1                     <>  650-604-2388 (VOICE) <>
<> NASA Ames Research Center     <>  650-604-3957 (FAX)   <>
<> Moffett Field, CA 94035-1000  <>                       <>
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>


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


Current Thread