5.1 - Decision making using <xsl:if>
| XML Document |
|---|
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="iliad05_1.xsl"?>
<document>
<header>
<title>Iliad</title>
<book>1.</book>
<verses>1-7</verses>
<translator>Lance Jenott</translator>
<date>
<month>April</month>
<year>2004</year>
</date>
</header>
<poem>
<line num="1">Sing, oh goddess, about the wrath of <name status="mortal">Achilles</name> son of <name status="mortal">Peleus</name>,</line>
<line num="2">the accursed wrath which cast a myriad of pain upon the <name status="mortal">Achaeans</name></line>
<line num="3">and sent forth many noble souls of heroes to <name status="divine">Hades</name>,</line>
<line num="4">making them prey for dogs and all birds.</line>
<line num="5">The will of <name status="divine">Zeus</name> was fulfilled,</line>
<line num="6">when, in fact, the two men first parted ways in strife,</line>
<line num="7">the son of <name status="mortal">Atreus</name>, ruler of men, and god-like <name status="mortal">Achilles</name>.</line>
</poem>
</document>
|
| Each of the <name> tags in this example contains a status attribute whose value in this example is either set to "mortal" or "divine". Attributes may be any information which the document owner wishes to provide. |
| XSLT script: iliad05_1.xsl |
|---|
<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="/"> <table border="1" cellpadding="3"> <th>Name of Character</th> <th>Line #</th> <xsl:for-each select="document/poem/line/name"> <xsl:if test="@status = 'divine'"> <tr></tr> <td> <xsl:value-of select="current()"/> </td> <td> <xsl:value-of select="../@num"/> </td> </xsl:if> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet> |
|
This script generates a table similar to the one in example 4, but here the <xsl:if test="@status = 'divine'"> command is employed in order to only show those names which have a status attribute set to "divine".
*Note that the text-string being tested is always set in single quotes ('divine') within the double quotes of the <xsl:if> command's test= attribute. |
| RESULTS of iliad05.xsl | ||||||
|---|---|---|---|---|---|---|
|
||||||
| Only names of divine characters are displayed. |
5.2 - Decision making with <xsl:choose> <xsl:when> <xsl:otherwise>
XSLT script: iliad5_2.xsl
<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<table border="1" cellpadding="3">
<th>Name of Character</th>
<th>Line #</th>
<xsl:for-each select="document/poem/line/name">
<xsl:choose>
<xsl:when test="@status = 'divine'">
<tr></tr>
<td>
<font color="purple"> <xsl:value-of select="current()"/> </font>
</td>
<td>
<xsl:value-of select="../@num"/>
</td>
</xsl:when>
<xsl:otherwise>
<tr></tr>
<td>
<font color="black"> <xsl:value-of select="current()"/> </font>
</td>
<td>
<xsl:value-of select="../@num"/>
</td>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Similar to <xsl:if>, the <xsl:choose> <xsl:when> and <xsl:otherwise> commands can be used to make decisions
in order to deal with various scenarios in different ways. As with <xsl:if>, each scenario is specified in the test= attribute
of the <xsl:when> command.
The <xsl:otherwise> command deals with cases where none of the <xsl:when> scenarios match the test.
In this example, when any <name> tag has a status attribute set to
"divine" the browser is told to display the text in purple; otherwise it is told to display the text in black.
RESULTS of iliad5_2.xsl
Name of Character
Line #
Achilles 1
Achaeans 2
Hades 3
Zeus 5
Atreus 7
Achilles 7
All names are displayed along with the line number on which they are found. Names of divine characters are
displayed in purple.