Re: [xsl] Conditional Formating

Subject: Re: [xsl] Conditional Formating
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 2 Jan 2001 18:29:20 +0000
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