XSLT <xsl:for-each> elements
May 28, 2021
XSL T
Table of contents
XSLT
-lt;xsl: for-each.gt;
elements
The XSLT-lt;xsl:for-each-gt; element loops in each specified node set.
XSLT element reference manual
Definitions and usages
The element can loop through each node in the specified node set.
Grammar
<xsl:for-each select="expression">
<!-- Content -->
</xsl:for-each>
Property
Attributes
|
value
|
describe
|
select
|
expression
|
Required.An XPath expression specifies that the selected node set.
|
The following example loops through each cd element and writes each cd to the output:
<?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>
The following instance loops through each cd element and creates a table showing the title and artist for each 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="/">
<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" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XSLT element reference manual