- XML基础教程:xsl:for-each 元素
-
XSLT < xsl:for-each> 元素
Definition and Usage
定义和用法The < xsl:for-each> element loops through each node in a specified node set.
< xsl:for-each>元素的作用是:在指定的节点组中循环操作每个节点。
Syntax
语法< xsl:for-eachselect="expression">
< !-- Content:(xsl:sort*,template) -->
< /xsl:for-each>
Attributes
属性属性 值 描述 select expression Required. The node set to be processed
必要参数。指定需要处理的节点组Example 1
案例1Loop 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>
查看XML 文件、XSL 文件以及结果。
Example 2
案例2Loop 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>
查看XML 文件、XSL 文件以及结果。
- 与本文主题相关的文章
