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

Re: Avoiding dummy xsl:if with apply-templates

Subject: Re: Avoiding dummy xsl:if with apply-templates
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Sun, 13 Feb 2005 15:24:00 +1100
xsl if condition or clause
On Sun, 13 Feb 2005 03:53:06 +0000, Frans Englich
<frans.englich@xxxxxxxxx> wrote:
> 
> Hello all,
> 
> I find myself struggling with a construct I often need: conditionally, "else",
> do something radically different depending on input.
> 
> For example:
> 
>           <xsl:variable name="el" select="elements" />
>            <xsl:if test="count($el)">
>                <ul>
>                    <xsl:apply-templates select="$el" />
>                </ul>
>            </xsl:if>
> 
> Here, the if statement and variable declaration exists solely to avoid an
> empty ul element; the special condition which occurs when the select misses.
> 
> Producing xhtml tables is a similar case. I find these common situations in
> XSLT programming.
> 
> These examples can be solved with usual conditional tests, as above, but I
> want to push the conditionalis upon the engine and write with templates; the
> clean, XSLT-like way. AFAICT, this made-up syntax would solve the problem:
> 
> <xsl:apply-templates select="elements">
>        <ul>
>                <xsl:apply/>
>        </ul>
> </xsl:apply-templates>
> 
> Hence, when the select clause fails, the apply-templates body is not entered,
> and the conditionalis is not needed to be manually written and comes
> naturally, even.
> 
> What is the proper way of doing what I want?


Something like this:

   <xsl:apply-templates select="elements[1]" mode="list">
     <xsl:with-param name="pElements" select="elements"/>
   </xsl:apply-templates>

Then the template to wrap the results in a "ul" is the following:

  <xsl:template match="element" mode="list">
     <xsl:param name="pElements"/>
     <ul>
         <xsl:apply-templates select="$pElements" mode="single"/>
    </ul>
  </xsl:template>



Cheers,
Dimitre Novatchev.

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.