<html>
<head>
<title>&lt;xsl:when&gt;</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div id="Description">
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="main">
<tr>
<td valign="top" class="NAME">&lt;xsl:when&gt;</td>
<td valign="top" class="COMPATIBILITY">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
</tr>
<tr>
<td valign="top" colspan="2" class="description">
Defines one branch of an <span class="LITERAL">&lt;xsl:choose&gt;</span> element.  It is equivalent to the Java <span class="LITERAL">case</span> statement.</td></tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Category</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>Subinstruction (<span class="LITERAL">&lt;xsl:when&gt;</span> always appears as a child of an <span class="LITERAL">&lt;xsl:choose&gt;</span> element)</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Required Attributes</td>
</tr>
<tr>
<td colspan="2" class="description">
<dl>
<dt>
test
</dt>
<dd>
Contains a boolean expression that is evaluated. If the expression evaluates to
<span class="LITERAL">
true
</span>
, the contents of the
<span class="LITERAL">
&lt;
xsl:when
&gt;
</span>
element are processed; otherwise, the contents of the
<span class="LITERAL">
&lt;
xsl:when
&gt;
</span>
are ignored.
<P></p>
</dl>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Optional Attributes</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>None.</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Content</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>An XSLT template.</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Appears in</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>The <span class="LITERAL">&lt;xsl:choose&gt;</span> element only. </p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Defined in</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>XSLT section 9.2, Conditional Processing with <span class="LITERAL">xsl:choose</span>.</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Example</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>This example uses an <span class="LITERAL">&lt;xsl:choose&gt;</span> element and three <span class="LITERAL">&lt;xsl:when&gt;</span> elements to cycle through a set of values.  Now we will generate rows of an HTML table for each <span class="LITERAL">&lt;listitem&gt;</span>:</p>
<span class="PROGRAMLISTING"><pre>
&lt;?xml version="1.0"?&gt;
&lt;list xml:lang="en"&gt;
  &lt;title&gt;Albums I've bought recently:&lt;/title&gt;
  &lt;listitem&gt;The Sacred Art of Dub&lt;/listitem&gt;
  &lt;listitem&gt;Only the Poor Man Feel It&lt;/listitem&gt;
  &lt;listitem&gt;Excitable Boy&lt;/listitem&gt;
  &lt;listitem xml:lang="sw"&gt;Aki Special&lt;/listitem&gt;
  &lt;listitem xml:lang="en-gb"&gt;Combat Rock&lt;/listitem&gt;
  &lt;listitem xml:lang="zu"&gt;Talking Timbuktu&lt;/listitem&gt;
  &lt;listitem xml:lang="jz"&gt;The Birth of the Cool&lt;/listitem&gt;
&lt;/list&gt;</pre></span>
<p>In our stylesheet, we'll generate table rows with the background colors of <span class="LITERAL">mintcream</span>, <span class="LITERAL">lavender</span>, <span class="LITERAL">whitesmoke</span>, and <span class="LITERAL">papayawhip</span>.  For each <span class="LITERAL">&lt;listitem&gt;</span> in our source document, one of the <span class="LITERAL">&lt;xsl:when&gt;</span> elements (or the <span class="LITERAL">&lt;xsl:otherwise&gt;</span> element) generates the appropriate color.</p>
<span class="PROGRAMLISTING"><pre>
&lt;?xml version="1.0"?&gt;
&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

<!--<?troff .Nd 10?>-->
  &lt;xsl:output method="html"/&gt;

  &lt;xsl:template match="/"&gt;
    &lt;html&gt;
      &lt;head&gt;
        &lt;title&gt;
          &lt;xsl:value-of select="list/title"/&gt;
        &lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
        &lt;h1&gt;&lt;xsl:value-of select="list/title"/&gt;&lt;/h1&gt;
        &lt;table border="1"&gt;
          &lt;xsl:for-each select="list/listitem"&gt;
            &lt;tr&gt;
              &lt;td&gt;
                &lt;xsl:attribute name="bgcolor"&gt;
                  &lt;xsl:choose&gt;
                    &lt;xsl:when test="@bgcolor"&gt;
                      &lt;xsl:value-of select="@bgcolor"/&gt;
                    &lt;/xsl:when&gt;
                    &lt;xsl:when test="position() mod 4 = 0"&gt;
                      &lt;xsl:text&gt;papayawhip&lt;/xsl:text&gt;
                    &lt;/xsl:when&gt;
                    &lt;xsl:when test="position() mod 4 = 1"&gt;
                      &lt;xsl:text&gt;mintcream&lt;/xsl:text&gt;
                    &lt;/xsl:when&gt;
                    &lt;xsl:when test="position() mod 4 = 2"&gt;
                      &lt;xsl:text&gt;lavender&lt;/xsl:text&gt;
                    &lt;/xsl:when&gt;
                    &lt;xsl:otherwise&gt;
                      &lt;xsl:text&gt;whitesmoke&lt;/xsl:text&gt;
                    &lt;/xsl:otherwise&gt;
                  &lt;/xsl:choose&gt;
                &lt;/xsl:attribute&gt;
                &lt;xsl:value-of select="."/&gt;
              &lt;/td&gt;
            &lt;/tr&gt;
          &lt;/xsl:for-each&gt;
        &lt;/table&gt;
      &lt;/body&gt;
    &lt;/html&gt;
  &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;
      </pre></span>
<!--<?troff .Nd 10?>-->
<p>When we process our XML source document with this stylesheet, here are the results:</p>
<span class="PROGRAMLISTING"><pre>
&lt;html&gt;
&lt;head&gt;
&lt;META http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;Albums I've bought recently:&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Albums I've bought recently:&lt;/h1&gt;
&lt;table border="1"&gt;
&lt;tr&gt;
&lt;td bgcolor="mintcream"&gt;The Sacred Art of Dub&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td bgcolor="lavender"&gt;Only the Poor Man Feel It&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td bgcolor="whitesmoke"&gt;Excitable Boy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td bgcolor="papayawhip"&gt;Aki Special&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td bgcolor="mintcream"&gt;Combat Rock&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td bgcolor="lavender"&gt;Talking Timbuktu&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td bgcolor="whitesmoke"&gt;The Birth of the Cool&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></span>
<p>All <span class="LITERAL">&lt;td&gt;</span> elements with a background color of <span class="LITERAL">papayawhip</span>, <span class="LITERAL">mintcream</span>, or <span class="LITERAL">lavender</span> were generated by one of the <span class="LITERAL">&lt;xsl:when&gt;</span> elements.</p>
</td>
</tr>
</table>
</div>
</body>
</html>