XSLT 教程
1. XSLT 教程 2. XSL 语言 3. XSLT 简介 4. XSLT 浏览器 5. XSLT 转换 6. XSLT <xsl:template> 元素 7. XSLT <xsl:value-of> 元素 8. XSLT <xsl:for-each> 元素 9. XSLT <xsl:sort> 元素 10. XSLT <xsl:if> 元素 11. XSLT <xsl:choose> 元素 12. XSLT <xsl:apply-templates> 元素 13. XSLT 在客户端 14. XSLT 在服务器端 15. XSLT – 编辑 XML 16. XSLT 总结 17. XSLT 实例 18. XSLT 元素参考手册 19. XSLT 函数

XSLT <xsl:for-each> 元素

XSLT <xsl:for-each> 元素


<xsl:for-each> 元素允许您在 XSLT 中进行循环。


<xsl:for-each> 元素

XSL <xsl:for-each> 元素可用于选取指定的节点集中的每个 XML 元素:

实例

<?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>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>

55面试教程网 »

注意:select 属性的值是一个 XPath 表达式。这个 XPath 表达式的工作方式类似于定位某个文件系统,在其中正斜杠(/)可选择子目录。


过滤输出结果

通过在 <xsl:for-each> 元素中添加一个选择属性的判别式,我们也可以过滤从 XML 文件输出的结果。

<xsl:for-each select="catalog/cd[artist='Bob Dylan']">

合法的过滤运算符:

  • = (等于)
  • != (不等于)
  • &lt; (小于)
  • &gt; (大于)

看看调整后的 XSL 样式表:

实例

<?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>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>

55面试教程网 »