Is it possible to provide a few values for the same param in XSL (vararg)?

Viewed 39

I'm trying to create an XSL 2.0 template, which I would be able to call with a few <xsl:with-param/> values, all of them bound to the same <xsl:param>. In other words, I'm looking for a variadic template or its best alternative. Can you suggest some?

ps. This is what I came up with:

<xsl:template name="i">
  <xsl:param name="args"/>
  <xsl:for-each select="$args/*">
    <xsl:value-of select="."/>
  </xsl:for-each>
</xsl:template>

Then, I'm calling it like this:

<xsl:template match="f">
  <xsl:call-template name="i">
    <xsl:with-param name="args"/>
      <a><xsl:value-of select="./@one"/></a>
      <a><xsl:value-of select="./@two"/></a>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>
1 Answers

There are many ways to do this, below are just three of them:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:param name="pParams" select="3,5,15,22,7"/>
 <xsl:param name="pParams2" select="'3,5,15,22,7'"/>
 <xsl:param name="pParams3" select="/*/*[1], /*/*[2], /*/*[3] "/>

  <xsl:template match="/*">
    <xsl:for-each select="$pParams">
      <xsl:sequence select=". * 2"/>
    </xsl:for-each>

    <xsl:value-of select="'&#xA;'"/>

    <xsl:for-each select="tokenize($pParams2, ',')">
      <xsl:sequence select="number(.) * 2"/>
    </xsl:for-each>
    
    <xsl:value-of select="'&#xA;'"/>
    
    <xsl:for-each select="$pParams3">
      <xsl:sequence select="name(.)"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

When this transformation is applied on this XML document:

<t>
  <a/>
  <b/>
  <c/>
</t>

the wanted result is produced:

6 10 30 44 14
6 10 30 44 14
a b c
Related