<xsl:template match="/catalog"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="gray"> <th>Title</th> <th>Artist</th> </tr> <xsl:apply-templates /> </table> </body> </html> </xsl:template> <xsl:template match="cd"> <tr> <td><xsl:apply-templates select="title" /></td> <td><xsl:apply-templates select="artist"/></td> </tr> </xsl:template> <xsl:template match="title"> <span style="font-family: roman; color: red"> <xsl:value-of select="."/> </span> </xsl:template> <xsl:template match="artist"> <xsl:value-of select="."/> </xsl:template>
<xsl:for-each select="cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each>This for-each statement returns all cd elements that are children of the current node.
<xsl:template match="cd"> <xsl:if test="price > 10"> ... </xsl:if> </xsl:template> <xsl:template match="cd"> <xsl:if test="country = 'UK'"> ... </xsl:if> </xsl:template>
<xsl:for-each select="cd">
<tr>
<td><xsl:value-of select="title"/>
<xsl:if test="price > 10"><sup>*</sup></xsl:if></td>
<td><ol>
<xsl:value-of select="artist" />
</xsl:for-each>
<td><xsl:if test="price > 10"><b></xsl:if> <xsl:value-of select="title" /> <xsl:if test="price > 10"></b></xsl:if> </td>This code represents an improperly nested piece of xml code because the <b> was not terminated before the <xsl:if> element. So even though the code would produce a properly nested html document if it were executed, the code will never get executed. To get the title to be properly bold-faced, I would need to use a choose statement:
<xsl:template match="cd"> <xsl:choose> <xsl:when test="country = 'UK'"> <tr> <td bgcolor="white"><xsl:apply-templates select="title" /></td> <td><xsl:apply-templates select="artist"/></td> </tr> </xsl:when> <xsl:when test="country = 'USA'"> ... </xsl:when> <xsl:otherwise> ... </xsl:otherwise> </xsl:choose> </xsl:template>
<xsl:for-each select="cd"> <xsl:sort select="artist" />
<xsl:sort select="artist" order="descending" />
<xsl:sort select="price" data-type="number" />
Valid values for data-type are text
and number (there is also something called a
qname but we will not cover it in this course).
<xsl:sort select="price" data-type="number" /> <xsl:sort select="artist" />
Code | Result |
---|---|
1 | 1 2 3 4 ... |
a | a b c d ... |
A | A B C D ... |
i | i ii iii iv ... |
I | I II III IV ... |
<xsl:for-each select="cd">
<xsl:number value="position()" format="1. "/>
... output ...