RE: [xsl] to access a particular token in a comma separated string by specifying a numerical value ?

Subject: RE: [xsl] to access a particular token in a comma separated string by specifying a numerical value ?
From: "Josh Canfield" <Josh.Canfield@xxxxxxxxxxxx>
Date: Mon, 26 Jan 2004 18:43:23 -0800
Here is a template that returns the string at the offset passed in the index param. An empty string is returned if the index is larger than the number of items in the list. If the index is less than 1 it always returns first element.

<xsl:variable name="ValuesString" select="'10,20,30,40,50'"/>
<xsl:variable name="ValueDelimiter" select="','"/>
<xsl:variable name="Index" select="4"/>

<xsl:template match="/">
  <xsl:variable name="item-at-index">
    <xsl:call-template name="getByIndex">
      <xsl:with-param name="index" select="$Index"/>
      <xsl:with-param name="list" select="$ValuesString"/>
      <xsl:with-param name="delim" select="$ValueDelimiter"/>
    </xsl:call-template>
  </xsl:variable>

  <selected>
  <xsl:value-of select="$item-at-index"/>
  </selected>
</xsl:template>

<xsl:template name="getByIndex">
  <xsl:param name="list"/>
  <xsl:param name="index" select="1"/>
  <xsl:param name="delim" select="','"/>

  <xsl:choose>
    <xsl:when test="string($list) != '' and number($index) &gt; 1">
        <!-- recurse discarding the top comma separated element 
             until there are no more commas, or we reach the 
             correct index -->
        <xsl:call-template name="getByIndex">
          <xsl:with-param name="index" select="$index - 1"/>
          <xsl:with-param name="list" select="substring-after($list,$delim)"/>
          <xsl:with-param name="delim" select="$delim"/>
        </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <!-- if there are remaining commas then truncate the 
           string, otherwise just return what remains of 
           the list (zero or one elements) -->
      <xsl:choose>
        <xsl:when test="contains($list,$delim)">
          <xsl:value-of select="substring-before($list,$delim)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$list"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Josh
-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of SANWAL,
ABHISHEK (HP-Houston)
Sent: Monday, January 26, 2004 3:36 PM
To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] to access a particular token in a comma separated string
by specifying a numerical value ?


How can I access a particular token in a comma separated string by specifying a numerical value for the token number using XSL?

Assuming, my xsl:variable or xsl:parameter holds a string with comma-separated values as shown below:

<xsl:variable name="ValuesString" select="'10,20,30,40,50'"/>

How would I go about finding a way to pick out the value specified by a count that maps another variable/parameter.

Open to Ideas :)
Thanks,

Abhishek 
____________________________________________________________

Abhishek Sanwal
HP - Houston Campus

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


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


Current Thread