<html>
<head>
<title>&lt;xsl:choose&gt;</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div id="Beschreibung">
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="main">
<tr>
<td valign="top" class="NAME">&lt;xsl:choose&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">
Das Element <span class="LITERAL">&lt;xsl:choose&gt;</span> ist die XSLT-Version der in vielen prozeduralen Programmiersprachen zu findenden <span class="LITERAL">switch</span>- oder <span class="LITERAL">case</span>-Anweisung. </td></tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Kategorie</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>Anweisung</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Obligatorische Attribute</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>Keine</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Optionale Attribute</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>Keine</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Inhalt</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>Das Element enth&auml;lt eines oder mehrere <span class="LITERAL">&lt;xsl:when&gt;</span>-Elemente und m&ouml;glicherweise auch ein einzelnes <span class="LITERAL">&lt;xsl:otherwise&gt;</span>-Element. Das Element <span class="LITERAL">&lt;xsl:otherwise&gt;</span> muss das letzte Element innerhalb der <span class="LITERAL">&lt;xsl:choose&gt;</span>-Anweisung sein.</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">&Uuml;bergeordnetes Element</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>
<span class="LITERAL">&lt;xsl:choose&gt;</span> erscheint innerhalb einer Vorlage. </p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Definition</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>XSLT-Abschnitt 9.2, Bedingte Verarbeitung mit <span class="LITERAL">xsl:choose</span></p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Beispiel</td>
</tr>
<tr>
<td colspan="2" class="description">

<p>Das nachfolgende Beispiel verwendet <span class="LITERAL">&lt;xsl:choose&gt;</span> dazu, die Hintergrundfarbe f&uuml;r die Zeilen in einer HTML-Tabelle auszuw&auml;hlen. Es werden vier verschiedene Werte durchschritten und <span class="LITERAL">&lt;xsl:choose&gt;</span> bestimmt den Wert des Attributs <span class="LITERAL">bgcolor</span> im erzeugten HTML-Dokument. Es wird das folgende XML-Dokument verwendet:</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> <!--<?troff .Nd 10?>-->
<p>Hier das Stylesheet:</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;

  &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="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>Mit Hilfe von <span class="LITERAL">&lt;xsl:choose&gt;</span> wird die Hintergrundfarbe von jedem <span class="LITERAL">&lt;td&gt;</span>-Element bestimmt.  Hier das erzeugte HTML-Dokument, das die verschiedenen Hintergrundfarben durchschreitet.</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>Wie das HTML-Dokument in einem Browser angezeigt wird, sehen Sie in <link linkend="xslt-appa-a5">Abbildung A-5</link>.</p>
<figure label="A-5" id="xslt-appa-a5">
        <p class="TITLE">Dokument-Cycling unter verschiedenen Hintergrundfarben</p>
        <graphic depth="248" width="455" fileref="figs/xslt.aa05.gif"/></figure>
</td>
</tr>
</table>
</div>
</body>
</html>
