Re: [xsl] xsl:when, xsl:otherwise question?

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

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

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

A stylesheet:

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

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

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

... 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!


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. 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