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

RE: Adding Variables

Subject: RE: Adding Variables
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Mon, 16 Aug 2004 14:20:28 +0100
adding variables in xslt
> 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

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.