[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: user functions with variable number of parameters

Subject: RE: user functions with variable number of parameters
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 10 Aug 2008 09:10:45 -0400
RE:  user functions with variable number of  parameters
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.