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

Re: Can templates be invoked based on the value of a

Subject: Re: Can templates be invoked based on the value of a variable?
From: Michael Müller-Hillebrand <mmh@xxxxxxxxxxxxx>
Date: Wed, 1 Apr 2009 22:14:34 +0200
Re:  Can templates be invoked based on the value of a
How about:

<xsl:template match="item1[$i = '1']">...

<xsl:template match="item2[$i = '2']">...

Would that work?

- Michael

Am 01.04.2009 um 21:52 schrieb pilgrim cnonline.net:

I would like to be able to invoke templates based on the value of a variable.

I've done some Internet searching, but haven't seen what I'm looking for, so
I don't know whether it's possible or not. My various attempts have failed.


My data has the structure:

<data>
   <item1>
       ...
   </item1>
   <item2>
       ....
   </item2>
</data>


I want to process the nodes <item1/> or <item2/>, but not both, depending on
a variable derived from some other processing. The outline of the XSL
processing
that I'd like to do is:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:template match="data">

<xsl:variable name="i">2</xsl:variable>

<!-- $item will have a value of either "item1" or "item2" -->
<xsl:variable name="item">item<xsl:value-of select="$i"/></ xsl:variable>


<!-- Want to invoke template for either item1 or item2, but
not both. -->
<!-- Would like to avoid using a 'choose' statement. -->
<!-- This does not work, but is conceptually what I'm thinking. -->
<xsl:apply-templates select="$item" />


</xsl:template>

   <xsl:template match="item1">
       ...
   </xsl:template>

   <xsl:template match="item2">
       ...
   </xsl:template>

</xsl:stylesheet>


I could use a <choose/> statement to do the selection based on the $item variable, but I'm wondering whether there is a more "elegant" way, or whether the <choose/> is the way to go even though the code is longer and "bulkier".

Thanks for suggestions.

-- Mike

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.