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

Re: XSL Choose inside a function??

Subject: Re: XSL Choose inside a function??
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 25 Oct 2002 00:56:08 +0100
xsl param choose
Hi Simon,

> I'm a xsl newbie, just to let you know.

But it looks as though you're using XSLT 2.0 rather than XSLT 1.0,
correct?

> my problem is, that I have to write a function with a if-else
> statement, so I tried it like this:
>
> <xsl:function name="xsl:getParam">
> <xsl:param name="class"/>
> <xsl:param name="param"/>
> <xsl:choose>
>   <xsl:when test="$class = '0'">
>     <xsl:choose>
>       <xsl:when test="$param = '0'">
>         <xsl:result select="/jsp_entity/data/bean/fields/pkey"/>
>       </xsl:when>
>       <xsl:otherwise>
>         <xsl:result select="/jsp_entity/data/bean/fields/parameter[position()=$param]"/>
>       </xsl:otherwise>
>     </xsl:choose>
>   </xsl:when>
>   <xsl:otherwise>
>     <xsl:result select="/jsp_entity/data/bean/classes/helper[position()=$class]/parameter[position()=$param]"/>
>   </xsl:otherwise>
> </xsl:choose>
> </xsl:function> 

xsl:choose isn't allowed inside xsl:function in XSLT 2.0. Instead, you
have to use XPath 2.0's if/then/else:

<xsl:function name="my:getParam">
  <xsl:param name="class" />
  <xsl:param name="param" />
  <xsl:result select="
    if ($class = '0') then
      if ($param = '0') then
        /jsp_entity/data/bean/fields/pkey
      else
        /jsp_entity/data/bean/fields/parameter[position() = $param]
    else
      /jsp_entity/data/bean/classes/helper[position() = $class]/parameter[position() = $param]" />
</xsl:function>

If you weren't returning a node, or if you were only interested in the
node's string value, or if a copy would do, then you could use
xsl:choose inside a variable and then return the value of the
variable -- something like:

<xsl:function name="xsl:getParam">
  <xsl:param name="class"/>
  <xsl:param name="param"/>
  <xsl:variable name="result">
    <xsl:choose>
      <xsl:when test="$class = '0'">
        <xsl:choose>
          <xsl:when test="$param = '0'">
            <xsl:copy-of select="/jsp_entity/data/bean/fields/pkey"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:copy-of select="/jsp_entity/data/bean/fields/parameter[position()=$param]"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="/jsp_entity/data/bean/classes/helper[position()=$class]/parameter[position()=$param]"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:result select="$result" />
</xsl:function>

You could use EXSLT's func:function (with XSLT 1.0) in exactly the way
that you have, with func:function inside the xsl:choose -- see
http://www.exslt.org/func. It's supported by Saxon, Xalan and various
other processors.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


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-2013 All Rights Reserved.