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

Re: Conditional Formating

Subject: Re: Conditional Formating
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 2 Jan 2001 18:29:20 +0000
xsl conditional format
Hi David,

> <xsl:template match="MONTH">
>  <xsl:choose>
>   <xsl:when test="MYFORMAT='WEEK'">
>    <td>You selected myformat of type week.</td>
>   </xsl:when>
>  </xsl:choose>
> </xsl:template>

Within this template, the context node is the 'MONTH' element.  All
the XPaths within the template will be resolved relative to that
element.  Your source tree looks something like:

+- (root)
   +- (element) CALENDAR
      | +- (attribute) MYFORMAT = WEEK  <-- you want to get here
      +- (element) MYFORMAT             <-- or here
      |  +- (text) WEEK
      +- (element) MONTHS
         +- (element) MONTH             <-- you are here
         +- (element) MONTH             <-- or here
         +- ...

There are two main ways to get from the MONTH element(s) to the
MYFORMAT attribute on the CALENDAR element or the MYFORMAT element
right under the CALENDAR element: you can go back up the tree from
where you are, or you can jump to the root and go down from there.

To go up the tree, first you need to go to the parent (MONTHS)
element using the XPath:

  parent::MONTHS

or (shorter and more efficient):

  ..

>From there, to get to the MYFORMAT *element*, you want the immediately
preceding sibling:

  ../preceding-sibling::*[1]

or, to phrase it another way, the preceding sibling called 'MYFORMAT':

  ../preceding-sibling::MYFORMAT

>From the MONTHS element to get to the CALENDAR element means again
getting the parent; from the CALENDAR element, you can go to the
MYFORMAT attribute along the attribute:: axis:

  ../../attribute::MYFORMAT

or (shorter):

  ../../@MYFORMAT

The alternative is to work from the top down.  Start from the root
node:

  /

then go to the CALENDAR element and thence to the MYFORMAT attribute:

  /CALENDAR/@MYFORMAT

or to the CALENDAR element and thence to the MYFORMAT element:

  /CALENDAR/MYFORMAT

These are just a few of the myriad ways of getting to the MYFORMAT
attribute or element (you could also use the preceding:: axis, or go
up and down the tree 15 times if you wanted).  Probably the best is to
go from the root node: i.e. use the test:
  
<xsl:template match="MONTH">
 <xsl:choose>
  <xsl:when test="/CALENDAR/MYFORMAT='WEEK'">
   <td>You selected myformat of type week.</td>
  </xsl:when>
 </xsl:choose>
</xsl:template>

As you're using this multiple times (it'll be calculated for each
month you have in the document), you may want to place the value of
the MYFORMAT in a global variable:

<xsl:variable name="myformat" select="/CALENDAR/MYFORMAT" />

<xsl:template match="MONTH">
 <xsl:choose>
  <xsl:when test="$myformat='WEEK'">
   <td>You selected myformat of type week.</td>
  </xsl:when>
 </xsl:choose>
</xsl:template>

[Note that you could use xsl:if rather than xsl:choose in the above,
but I guess you're going to add more xsl:whens.]

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.