Re: [xsl] XSL Choose inside a function??

Subject: Re: [xsl] XSL Choose inside a function??
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 25 Oct 2002 00:56:08 +0100
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