XSLT <xsl:sort> 元素
<xsl:sort> 元素用於對輸出進行排序。
排序資訊放置位置
要對輸出進行排序,只需在 XSL 檔案中的 <xsl:for-each> 元素內新增一個 <xsl:sort> 元素即可
示例
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>我的 CD 收藏</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>標題</th>
<th>藝術家</th>
</tr>
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
自己動手試一試 »
注意: select 屬性表示要排序的 XML 元素。