Re: comma separated data

Subject: Re: comma separated data
From: Mike Brown <mike@xxxxxxxx>
Date: Thu, 6 Apr 2000 13:51:55 -0600 (MDT)
> I need to separate the following data into separate fields:
>  
> <years  title = "year">1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
> 2003,   </year>
>  
> For instance, the output needs to read:
>  
> <tr><td>1994</td><td>1995</td><td>1996</td> ... etc ... </tr>
>  
> I have seen this done using MS IE 5.0 and the DOM, but was wondering if it
> was possible using *just* XSL?

Of course.

<xsl:template match="years">
  <tr>
    <xsl:call-template name="split_years">
      <xsl:with-param name="year_list" select="text()"/>
    </xsl:call-template>
  </tr>
</xsl:template>

<xsl:template name="split_years">
  <xsl:param name="year_list"/>
  <xsl:choose>
    <!-- if there's a comma, take everything before it... -->
    <xsl:when test="contains($year_list,',')">
      <xsl:variable name="year" select="normalize-space(substring-before($year_list,','))"/>
      <!-- only make a table cell if there is non-whitespace before the comma -->
      <xsl:if test="$year">
        <td>
          <xsl:value-of select="$year"/>
        </td>
      </xsl:if>
      <!-- ...then go process everything after it -->
      <xsl:call-template name="split_years">
        <xsl:with-param name="year_list" select="substring-after($year_list,',')"/>
      </xsl:call-template>
    </xsl:when>
    <!-- if there's no comma, but something besides whitespace -->
    <xsl:when test="normalize-space($year_list)">
      <td><xsl:value-of select="$year_list"/></td>
    </xsl:when>
    <!-- otherwise, $year_list must be empty, so do nothing-->
  </xsl:choose>
</xsl:template>


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


Current Thread