XSLT <xsl:for-each> 元素
定義和用法
<xsl:for-each> 元素可遍歷指定的節點集中的每個節點。
語法
<xsl:for-each select="expression"> <!-- Content:(xsl:sort*,template) --> </xsl:for-each>
屬性
屬性 | 值 | 描述 |
---|---|---|
select | expression | 必需。被處理的節點集。 |
實例
例子 1
循環遍歷每個 "cd" 元素,并使用 <xsl:value-of> 把每個 title 和 artist 寫到輸出中:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <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>
查看 XML 文件,查看 XSL 文件,查看結果。
例子 2
循環遍歷每個 "cd" 元素,并使用 <xsl:value-of> 把每個 title 和 artist 寫到輸出中(以 artist 排序):
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</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>
查看 XML 文件,查看 XSL 文件,查看結果。