Updated April 6, 2023
Introduction of XSLT for each
XSLT for each is defined as the iteration process that allows retrieving data from the specified nodes. Based on the selection criteria defined with these functions, it makes a loop through multiple nodes. This works in adding up with the function <xsl:value-of>. It repeats the block of content over each element in the given node-set.
Syntax
Syntax of this function is given as
<xsl: for-each
select = Expression >
</xsl: for-each>
The expression here defines either the Xpath or XSLT variable.
How for each function work in XSLT?
Working of this for-each function is very simple as it is very similar to for loop statements in other programming Languages, which causes iterations over the list of items in the sequence. This function requires a select expression as shown in the Syntax to select the required node without overlapping (means using a child axis, not the descendant axis for iteration). Normally iteration of the node is done in the order kept originally to prefer rearrangement of the nodes XSL: sort is used with the self-closing element declaration. Whereas a for-each element requires an opening and closing element. Eventually, the two techniques have been achieved here.
- Performing inside the body of for-each
- Second, by using recursion (Does the exit immediately).
Using node-set, we can extract the elements in the document. The format is given as
<xsl:for-each select="*">
<xsl:value-of select="." />
</xsl:for-each>
Here we have shown how to iterate over XML nodes. To be more precise, let’s see how the names in the order is encountered. Let’s say our XML file as
<patients><Floor>
<Roomno>
Our Stylesheet goes like this
<xsl: for-each select="/patients/Floor/roomno">
<item><xsl:value-of select=". /name"/></item>
</xsl: for-each>
Next, we do for-each within the variable element.
<xsl:variable name="myexample">
<xsl:choose>
<xsl:when test="string-length(educba.package:RequestQueryString('myexample')) < 0">
<xsl:value-of select="educba.package:RequestQueryString('myexample')" />
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="...">
...
</xsl:for-each>
</xsl:otherwise>
</xsl:variable>
If it is semi-intensive, then it is clear to process this function and store the result in its variable, next with the iteration. And there is no break statement in XSLT for each to stop the condition. The other option would be to include ‘select’ in for-each as shown above. The body of the statement is executed unless the condition is true as it still iterates through all the nodes. This is very equivalent to the break statement.
<xsl:for-each select="...node set...">
<xsl:if test="...check condition..">
// body statement of the loop
Or
// To add up a particular condition we can apply like:
<xsl:for-each select="preceding-sibling::node[condition exp]">
</xsl:if>
</xsl:for-each>
The next case is using Group-by attribute with for-each
<xsl:for-each-group select="root/child" group-by="@child element">
<xsl:comment>
<xsl:value-of select="current-grouping-key()"/>
</xsl:comment>
In the next section, we can see the examples usage of function for each below. Let’s get started.
Examples
Here are the following examples mention below
Example #1
Function and table rows
XML
<?xml version="1.0" encoding="UTF-8"?>
<Banking>
<Topbank id="123">
<foreName>Giana</foreName>
<SurName>Amreal</SurName>
<HomeAddress>USA</HomeAddress>
</Topbank>
<Topbank id="456">
<foreName>Chris</foreName>
<SurName>Houtson</SurName>
<HomeAddress>Sweden</HomeAddress>
</Topbank>
<Topbank id="654">
<foreName>Dallni</foreName>
<SurName>Furaq</SurName>
<HomeAddress>Australia</HomeAddress>
</Topbank>
<Topbank id="334">
<foreName>Deutsche</foreName>
<SurName>Bank</SurName>
<HomeAddress>Germany</HomeAddress>
</Topbank>
</Banking>
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> Top Bank List</h2>
<table border="2">
<tr bgcolor="pink">
<th>foreName</th>
<th>HomeAddress</th>
</tr>
<xsl:for-each select="Banking/Topbank">
<tr>
<td><xsl:value-of select="foreName" /></td>
<td><xsl:value-of select="HomeAddress" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Explanation
The example creates a table by selecting the Banking element within which the values are iterated.
Output:
Example #2
XML
<?xml version="1.0" standalone="no"?>
<Blogs>
<Blog>
<Article>XSLT Variable</Article>
<Article>Python forloop</Article>
<link>https://www.educba.com/XSLT/art</link>
</Blog>
<Blog>
<Article>Database Tutorial</Article>
<Article>Normalization Implementation</Article>
<link>https://www.etex.com/database/art</link>
</Blog>
<Blog>
<Article>Artificial Intelligence</Article>
<Article>Neural Connectivity</Article>
<link>https://www.oreee.com/Machine/art</link>
</Blog>
</Blogs>
XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Blog">
<xsl:for-each select="Article">
<xsl:value-of select="."/>
<xsl:element name="br"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Explanation
So here we are extracting the Article element, which is declared twice in the XML document and their value is iterated in the following Output.
Output:
Example #3
XML
<?xml version="1.0"?>
<storeBay>
<supermarket>
<sid>501</sid>
<sname> Wegmans</sname>
<product> grocery</product>
<branch> two</branch>
<location> new York</location>
</supermarket>
<supermarket>
<sid>540</sid>
<sname> Market basket</sname>
<product> Burger</product>
<branch> seven</branch>
<location>New England</location>
</supermarket>
<supermarket>
<sid>301</sid>
<sname> Wallmart</sname>
<product> Icecream</product>
<branch> fifteen</branch>
<location> France</location>
</supermarket>
</storeBay>
XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="storeBay">
<h2>
<xsl:for-each select="supermarket">
<xsl:sort select="location"/>
<p>SNAME: <xsl:value-of select="sname"/></p>
<p>LOCATION: <xsl:value-of select="location"/></p>
<hr/>
</xsl:for-each>
</h2>
</xsl:template>
</xsl:stylesheet>
Explanation
The above example shows a template for a supermarket element that process all the child element <sname> and <location> in the order using sort element.
Output:
Example #4
Using For-each iteration through each tag
XML
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="xxx.xsl" ?>
<Experts>
<Expert>
<Ename>Jurgen Stefan</Ename>
<Designation>Technical Writer.</Designation>
<Country>WA</Country>
<phone>(201) 436-4857</phone>
</Expert>
<Expert>
<Ename>Bernard Carlin</Ename>
<Designation>Team Leader.</Designation>
<Country>Germany</Country>
<phone>(325) 112-4957</phone>
</Expert>
<Expert>
<Ename>Schaffer Walden</Ename>
<Designation>Manager.</Designation>
<Country>New York</Country>
<phone>(602) 236-1647</phone>
</Expert>
<Expert>
<Ename>Karsten Emery</Ename>
<Designation>Data Analyst</Designation>
<Country>Oslo</Country>
<phone>(666) 316-6847</phone>
</Expert>
<Expert>
<Ename>Drake Frieda</Ename>
<Designation>Data Engineer.</Designation>
<Country>Denmark</Country>
<phone>(201) 436-4857</phone>
</Expert>
<Expert>
<Ename>Uben Geeorge</Ename>
<Designation>Story Boarder.</Designation>
<Country>California</Country>
<phone>(201) 436-4857</phone>
</Expert>
</Experts>
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<HTML>
<BODY>
<TABLE>
<xsl:for-each select="Experts/Expert">
<xsl:sort select="Country" order="descending"/>
<xsl:sort select="Ename"/>
<TR>
<TD><xsl:value-of select="Ename" /></TD>
<TD><xsl:value-of select="Designation" /></TD>
<TD><xsl:value-of select="phone" /></TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
Explanation
That’s all we need to do, looping through this value with the html Body content along with the row table.
Output:
Example #5
XML
<?xml version="1.0"?>
<Softwares>
<Software id="s1" name="Winzip" price="3250" stock="2" country="Norway"/>
<Software id="s2" name="Odongo" price="1000" stock="4" country="Austria"/>
<Software id="s3" name="Anti-virus" price="1200" stock="18" country="Netherland"/>
<Software id="s4" name="Adobe" price="1500" stock="4" country="Quatar"/>
<Software id="s5" name="Visual Studio" price="1225" stock="3" country="Japan"/>
</Softwares>
XSL
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<Softwares>
<xsl:for-each-group select="Softwares/Software" group-by="@country">
<xsl:comment>
<xsl:value-of select="current-grouping-key()"/>
</xsl:comment>
<xsl:for-each select="current-group()">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:for-each-group>
</Softwares>
</xsl:template>
</xsl:stylesheet>
Explanation
Here in the XSLT function, we have used Group-by for each statement that selects the country attribute and does the iteration for the software element.
Output:
Advantages
- The child node that is to be selected is known in advance.
- They are been the best to iterate the elements without breaking. The better option is to use ‘select’.
- Even we can use ‘filter’ in this function.
Conclusion
In this article, we had a closer look at the function for each, considering many traditional syntaxes with the parameters and their implementation in different cases.
Recommended Articles
This is a guide to XSLT for each. Here we discuss How for each function work in XSLT, along with the examples and outputs. You may also have a look at the following articles to learn more –