Comma separated string parsing XSLT

xslt

How can I loop through a Comma separated string which I am passing as a parameter in XSLT 1.0?
Ex-

<xsl:param name="UID">1,4,7,9</xsl:param>

I need to loop the above UID parameter and collectd nodes from within each of the UID in my XML File

Best Answer

Vanilla XSLT 1.0 can solve this problem by recursion.

<xsl:template name="split">
  <xsl:param name="list"      select="''" />
  <xsl:param name="separator" select="','" />

  <xsl:if test="not($list = '' or $separator = '')">
    <xsl:variable name="head" select="substring-before(concat($list, $separator), $separator)" />
    <xsl:variable name="tail" select="substring-after($list, $separator)" />

    <!-- insert payload function here -->

    <xsl:call-template name="split">
      <xsl:with-param name="list"      select="$tail" />
      <xsl:with-param name="separator" select="$separator" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

There are pre-built extension libraries that can do string tokenization (EXSLT has a template for that, for example), but I doubt that this is really necessary here.

Related Topic