Welcome to The Coding College! In this tutorial, we’ll explore the <xsl:for-each>
element in XSLT, a powerful tool for iterating through nodes in an XML document. This element allows you to process multiple nodes, making it essential for dynamic transformations.
What Is the <xsl:for-each>
Element?
The <xsl:for-each>
element is used in XSLT to loop through a set of nodes selected using an XPath expression. For each node in the selected set, the XSLT processor applies the code inside the <xsl:for-each>
block, enabling dynamic and repetitive transformations.
Syntax
<xsl:for-each select="XPath_expression">
<!-- Instructions to apply to each node -->
</xsl:for-each>
Attributes:
select
: Specifies the nodes to iterate over using an XPath expression.
How <xsl:for-each>
Works
- The
select
attribute identifies a set of nodes in the XML document. - The XSLT processor processes each node in the set sequentially.
- The instructions inside the
<xsl:for-each>
block are executed for each node.
Example: Iterating Through Nodes
Input XML:
<bookstore>
<book>
<title>Learning XML</title>
<author>John Doe</author>
<price>29.99</price>
</book>
<book>
<title>Advanced XSLT</title>
<author>Jane Smith</author>
<price>39.99</price>
</book>
</bookstore>
XSLT Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<h1>Bookstore</h1>
<xsl:for-each select="bookstore/book">
<div>
<h2><xsl:value-of select="title" /></h2>
<p><strong>Author:</strong> <xsl:value-of select="author" /></p>
<p><strong>Price:</strong> $<xsl:value-of select="price" /></p>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output HTML:
<html>
<body>
<h1>Bookstore</h1>
<div>
<h2>Learning XML</h2>
<p><strong>Author:</strong> John Doe</p>
<p><strong>Price:</strong> $29.99</p>
</div>
<div>
<h2>Advanced XSLT</h2>
<p><strong>Author:</strong> Jane Smith</p>
<p><strong>Price:</strong> $39.99</p>
</div>
</body>
</html>
Key Features of <xsl:for-each>
1. Iterating Through Node Sets
The select
attribute specifies the nodes to process. Each node is processed in the order it appears in the XML document.
2. Accessing Node Data
Within the <xsl:for-each>
block, you can use XPath to access the current node’s children or attributes.
3. Using Context Information
Inside <xsl:for-each>
, you can use the position()
function to get the current index of the node in the iteration.
Example: Adding Node Numbers
Input XML:
<products>
<product>
<name>Smartphone</name>
<price>699.99</price>
</product>
<product>
<name>Laptop</name>
<price>999.99</price>
</product>
</products>
XSLT Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<h1>Product List</h1>
<ul>
<xsl:for-each select="products/product">
<li>
<xsl:value-of select="position()" />.
<strong><xsl:value-of select="name" /></strong> - $<xsl:value-of select="price" />
</li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output HTML:
<html>
<body>
<h1>Product List</h1>
<ul>
<li>1. <strong>Smartphone</strong> - $699.99</li>
<li>2. <strong>Laptop</strong> - $999.99</li>
</ul>
</body>
</html>
Example: Sorting Nodes
You can sort nodes within <xsl:for-each>
using the <xsl:sort>
element.
Input XML:
<students>
<student>
<name>John</name>
<score>85</score>
</student>
<student>
<name>Jane</name>
<score>92</score>
</student>
</students>
XSLT Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<h1>Student Scores</h1>
<ul>
<xsl:for-each select="students/student">
<xsl:sort select="score" order="descending" />
<li>
<strong><xsl:value-of select="name" /></strong>: <xsl:value-of select="score" />
</li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output HTML:
<html>
<body>
<h1>Student Scores</h1>
<ul>
<li><strong>Jane</strong>: 92</li>
<li><strong>John</strong>: 85</li>
</ul>
</body>
</html>
Best Practices
- Use Efficient XPath Expressions:
Avoid selecting unnecessary nodes to improve performance. - Combine with
<xsl:sort>
for Ordering:
Use<xsl:sort>
within<xsl:for-each>
to sort nodes dynamically. - Limit Complex Logic:
Keep the logic inside<xsl:for-each>
simple. For complex transformations, consider combining it with templates or named templates.
Limitations of <xsl:for-each>
- Less Reusability: Logic within
<xsl:for-each>
is not reusable. Consider using<xsl:apply-templates>
for modularity. - Limited Scope for Formatting:
<xsl:for-each>
focuses on processing, so combine it with other XSLT elements for better output formatting.
Conclusion
The <xsl:for-each>
element is an invaluable tool for processing multiple nodes in XML documents. Whether you’re generating lists, iterating over datasets, or applying dynamic transformations, <xsl:for-each>
provides the flexibility you need.