天天看點

XSLT <xsl:for-each> 元素

XSLT 元素參考手冊

定義和用法

<xsl:for-each> 元素可循環周遊指定的節點集中的每個節點。

文法

<xsl:for-each

select="expression">

<!-- Content -->

</xsl:for-each>

屬性

描述
select expression 必需。一個 XPath 表達式,規定被處理的節點集。

執行個體

下面的執行個體循環周遊每個 cd 元素,并把每個 cd 寫到輸出中:

<?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="/">

<div>

<xsl:for-each select="catalog/cd">

<p><xsl:value-of select="title" /></p>

</xsl:for-each>

</div>

</xsl:template>

</xsl:stylesheet>

下面的執行個體循環周遊每個 cd 元素,并建立一個表格顯示每個 cd 的 title 和 artist:

<html>

<body>

<h1>Music Collection:</h1>

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

</xsl:for-each>

</table>

</body>

</html>

XSLT 元素參考手冊