RE: [xsl] user functions with variable number of parameters

Subject: RE: [xsl] user functions with variable number of parameters
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 10 Aug 2008 09:10:45 -0400
At 2008-08-09 23:05 -0500, Bordeman, Chris wrote:
Not quite what I want, need a little more logic.  I've ALMOST got it
now.  I've figured out that I can just use a normal untyped parameter,
pass it as a set, such as:

fn:myconcat(('abc', '123', 'def'))

In the function figured out to use for-each on the parameter, but now
trying to figure out how to tell which element # I'm on inside the
for-each, especially to detect the last item.  Also need to discard the
'current' item if it's empty, which I can't quite get.

Then just build the return sequence or the return values incrementally for each item. Below is an example where I inspect each item and treat them differently, with attention to the last item. I've copied the same logic in both functions ... you could just as well have the concat function call the sequence function using string-join().


I hope this helps.

. . . . . . . . Ken

T:\ftemp>type chris.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xsd="http://www.w3.org/2001/XMLSchema";
                exclude-result-prefixes="xsd"
                xmlns:c="urn:x-Chris"
                version="2.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:value-of select="c:mysequence(('abc', '123', 'def'))"/>
  <xsl:text>
</xsl:text>
  <xsl:value-of select="c:myconcat(('abc', '123', 'def'))"/>
</xsl:template>

<xsl:function name="c:mysequence">
  <xsl:param name="args"/>

  <xsl:for-each select="$args">
    <xsl:choose>
      <xsl:when test=". castable as xsd:decimal">
        <xsl:sequence select="xsd:decimal(.) * 2"/>
      </xsl:when>
      <xsl:when test="position()=last()">
        <xsl:sequence select="concat(.,.)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:sequence select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each>
</xsl:function>

<xsl:function name="c:myconcat">
  <xsl:param name="args"/>

  <xsl:for-each select="$args">
    <xsl:choose>
      <xsl:when test=". castable as xsd:decimal">
        <xsl:value-of select="xsd:decimal(.) * 2"/>
      </xsl:when>
      <xsl:when test="position()=last()">
        <xsl:value-of select="concat(.,.)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each>
</xsl:function>

</xsl:stylesheet>
T:\ftemp>xslt2 chris.xsl chris.xsl con
abc 246 defdef
abc246defdef
T:\ftemp>


-- Upcoming XSLT/XSL-FO hands-on courses: Wellington, NZ 2009-01 World-wide corporate, govt. & user group XML, XSL and UBL training RSS feeds: publicly-available developer resources and training G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread