RE: [xsl] Adding Variables

Subject: RE: [xsl] Adding Variables
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Mon, 16 Aug 2004 14:20:28 +0100
> What I've never quite understood is how to wrap this into a 
> conditional.
> 
> Based on http://www.dpawson.co.uk/xsl/sect2/N8090.html#d9711e895 
> example, would this be correct for wrapping an assignment in 
> only if a 
> variable was passed in, otherwise using the default?
> 
> <xsl:variable name="n">
>    <!--Conditionally instantiate a value to be assigned to 
> the variable 
> -->
>    <xsl:choose>
>      <xsl:when test=="//test1/somevalue1">
>        <xsl:variable name="num1" select="//test1/somevalue1"/>
>      </xsl:when>
>      <xsl:otherwise>
>        <xsl:value-of select="20"/><!-- ...or a "20" -->
>      </xsl:otherwise>
>    </xsl:choose>
> </xsl:variable>
> 

The problem with this code is that you get a copy of the nodes in
somevalue1. If you want the variable to hold references to the actual nodes,
a conditional assignment is difficult to achieve in XSLT 1.0. It becomes
easy in 2.0:

<xsl:variable name="n" select="if (COND) then //a/b/c else 20"/>

In 1.0 you can sometimes solve the problem with

<xsl:variable name="n" select="//a/b/c[COND] | //a/b/d[not(COND)]"/>

Michael Kay

Current Thread