xsl-list
[Top] [All Lists]

Re: xsl:when, xsl:otherwise question?

2005-02-03 13:18:22
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[(_at_)n &lt; 100]">
  <xsl:text>An element was found with n less than 100</xsl:text>
</xsl:template>

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

<xsl:template match="element[(_at_)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(_at_)mulberrytech(_dot_)com
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
======================================================================


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--