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

Re: xsl:when, xsl:otherwise question?

Subject: Re: xsl:when, xsl:otherwise question?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 03 Feb 2005 15:18:22 -0500
xsl when otherwise
Nick,

At 02:24 PM 2/3/2005, you wrote:
Is it possible to implement an else-if condition using the above.

All literature i've read only seems to use xsl:when and xsl:otherwise... no "xsl:else-when" condition?

xsl:when/xsl:otherwise *is* an else-when construction ... you can have as many xsl:when conditionals as you like, plus an xsl:otherwise to catch where none of the xsl:when conditions apply.


But the post is puzzling to me for another reason ... earlier you asked about how to switch between colors using a conditional, and two experts (Mike and David C) showed you what the cleanest simplest way to do it would be, using templates. Templates themselves *are* a kind of conditional ("when X matches, do this, when Y matches, do that"), and they work very well -- so well that stylesheets can execute very complex processing logic, all of it conditional on the input, without having a choose/when/otherwise construct in sight.

You haven't told us why you can't do it this way in your code, which leaves it an open question why you need to know that when/otherwise is exactly what you think it isn't. :-> If we knew more about what you were trying to do and why the tried-and-true way won't work, we might be able to provide better advice.

An input document

<doc>
  <element n="10"/>
  <element n="100"/>
</doc>

A stylesheet:

<xsl:template match="element[@n &lt; 100]">
  <xsl:text>An element was found with n less than 100</xsl:text>
</xsl:template>

<xsl:template match="element[@n = 100]">
  <xsl:text>An element was found with n equal to 100</xsl:text>
</xsl:template>

<xsl:template match="element[@n &gt; 100]">
  <xsl:text>An element was found with n greater than 100</xsl:text>
</xsl:template>

... this works like an if/else-if/else-if. (This does raise some interesting questions like what happens when more than one template matches. We're here partly to help you answer those questions.)

If you don't understand why this works, you have some homework to do: just about everything in XSLT will be hard until you grasp template matching. (When you do, it becomes a lot more fun, and mostly pretty easy.)

If you do understand it, but something about your problem still evades us, please explain so we can do a better job!

Cheers,
Wendell


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================

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.