Beispiel 3 für <xsl:if>

Mit der folgenden item-Vorlagenregel werden alle anderen Tabellenzeilen gelb markiert.

XML-Datei (items.xml)

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="ifyellow.xsl" ?>
<items>
   <item>Car</item>
   <item>Pen</item>
   <item>LP Record</item>
   <item>Wisdom</item>
   <item>Cell phone</item>
   <item>Film projector</item>
   <item>Hole</item>
   <item>Canopy</item>
   <item>Widget</item>
   <item>Concept</item>
   <item>Null character</item>
</items>

XSLT-Datei (ifyellow.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="/">
<html>
<body>
<table border="1" cellpadding="2" cellspacing="0" width="50%">
<xsl:apply-templates/>
</table>

</body>
</html>
</xsl:template>

<xsl:template match="item">
  <tr>
    <xsl:if test="position() mod 2 = 0">
       <xsl:attribute name="bgcolor">yellow</xsl:attribute>
    </xsl:if>
    <xsl:apply-templates/>
  </tr>
</xsl:template>

</xsl:stylesheet>

Ausgabe

Dies ist die formatierte Ausgabe:

Formatierte Ausgabe

Dies ist die Prozessorausgabe:

<html>

<body>

<table border="1" cellpadding="2" cellspacing="0" width="50%">

<tr>Car</tr>

<tr bgcolor="yellow">Pen</tr>

<tr>LP Record</tr>

<tr bgcolor="yellow">Wisdom</tr>

<tr>Cell phone</tr>

...

</table>

</body>

</html>

Siehe auch

Konzepte

Beispiel 1 für <xsl:if>

Beispiel 2 für <xsl:if>

Beispiel 4 für <xsl:if>