Freigeben über


Example 3 of <xsl:if> 

The following item template rule colors every other table row yellow.

XML File (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 File (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>

Output

This is the formatted output:

Formatted output

This is the processor output:

<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>

See Also

Concepts

Example 1 of <xsl:if>
Example 2 of <xsl:if>
Example 4 of <xsl:if>