当前位置: 首页 > 网络学院 > XML相关教程 > XSL/XSLT > XSLT <xsl:for-each> 元素
The <xsl:for-each> element loops through each node in a specified node set.
<xsl:for-each>元素的作用是:在指定的节点组中循环操作每个节点。
<xsl:for-each select="expression"> <!-- Content:(xsl:sort*,template) --> </xsl:for-each> |
属性 | 值 | 描述 |
---|---|---|
select | expression | Required. The node set to be processed 必要参数。指定需要处理的节点组 |
Loop through each "cd "element and use <xsl:value-of> to write each title and artist to the output:
循环操作每个“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> |
Loop through each "cd "element and use <xsl:value-of> to write each title and artist to the output (sorted by artist):
循环操作每个“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> |