EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial XSLT for each

XSLT for each

Updated April 6, 2023

xslt-for-each

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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')) &lt; 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:

XSLT for each output 1

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:

XSLT for each output 2

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:

XSLT for each output 3

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:

output 4

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:

output 5

Advantages

  1. The child node that is to be selected is known in advance.
  2. They are been the best to iterate the elements without breaking. The better option is to use ‘select’.
  3. 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 –

  1. Oracle XML
  2. XML ampersand
  3. XML XSD
  4. XML HttpRequest 
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Software Development Bundle5000+ Hours of HD Videos | 149 Learning Paths | 1050+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more