XSLT <xsl:for-each> 元素
<xsl:for-each> 元素允許您在 XSLT 中進(jìn)行循環(huán)。
<xsl:for-each> 元素
<xsl:for-each> 元素可用于選取指定的節(jié)點(diǎn)集中的每個 XML 元素。
<?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>
注釋:select 屬性的值是一個 XPath 表達(dá)式。此表達(dá)式的工作方式類似于定位某個文件系統(tǒng),在其中正斜杠可選擇子目錄。
上面的轉(zhuǎn)換結(jié)果類似這樣:

結(jié)果過濾
通過在 <xsl:for-each> 元素中添加一個選擇屬性的判別式,我們也可以過濾從 XML 文件輸出的結(jié)果。
<xsl:for-each select="catalog/cd[artist='Bob Dylan']
">
合法的過濾運(yùn)算符:
- = (等于)
- != (不等于)
- < (小于)
- > (大于)
<?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[artist='Bob Dylan']">
<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>
上面的轉(zhuǎn)換結(jié)果類似這樣:
