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

Subject: RE: [xsl] XSL Choose inside a function??
From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx>
Date: Fri, 25 Oct 2002 10:29:59 +0100
> Hello!
> 
> I'm a xsl newbie, just to let you know.
> my problem is, that I have to write a function with a if-else 
> statement, so I tried it like this:

This looks like an XSLT 2.0 question, so the first thing to say is that
XSLT 2.0 is still a working draft and liable to change, and it's not
really what newbies should be using yet.

The simple way to write your code is to use the XPath 2.0 conditional
expression:

<xsl:result select="
    if ($param = '0')
    then /jsp_entity/data/bean/fields/pkey
    else /jsp_entity/data/bean/fields/parameter[position()=$param]"/>

The reason xsl:function is very restrictive about what instructions you
can use is to ensure that functions are not able to write to the result
tree. This makes it far easier to optimize XPath expressions that
contain function calls, because the function call has no side-effects.

At one stage the spec imposed this restriction dynamically (allowing you
to use xsl:choose and xsl:call-template provided that you didn't invoke
things like xsl:element), but having a syntactic restriction allows much
better error detection and reporting.

Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx 
 
> 
> <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> 
> 
> but the compiler told me this:
> 
> Error at xsl:choose on line 8 of file:/D:/e188/jsp_entity.xsl:
>   This element cannot be used within xsl:function
> Error at xsl:choose on line 8 of file:/D:/e188/jsp_entity.xsl:
>   Element must only be used within a template body
> Error at xsl:result on line 12 of file:/D:/e188/jsp_entity.xsl:
>   xsl:result must have xsl:function as its parent
> Error at xsl:result on line 15 of file:/D:/e188/jsp_entity.xsl:
>   xsl:result must have xsl:function as its parent
> Error at xsl:result on line 20 of file:/D:/e188/jsp_entity.xsl:
>   xsl:result must have xsl:function as its parent
> 
> So my question is, is it really not possible to use a choose 
> in a function??
> 
> Thank Alot!! 
> ________________________________________________________________
> Keine verlorenen Lotto-Quittungen, keine vergessenen Gewinne mehr! 
> Beim WEB.DE Lottoservice: http://tippen2.web.de/?x=13
> 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


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


Current Thread