Let's learn XSLT through examples. This is the continuation of the previous article Workday XSLT 06- Basic XSLT Examples (Format Dates and Numbers).
YouTube Video
Online XSLT Editor
https://www.freeformatter.com/xsl-transformer.html
XSLT: For-Each-Group
XML
<?xml version="1.0" encoding="UTF-8"?> <Root xmlns:ab="http://www.w3.org/TR/html4/"> <ab:Row> <ab:Employee_ID>1001</ab:Employee_ID> <ab:Name Type="Full Name">Emma Dylan</ab:Name> <ab:Hire_Date>2019-01-13</ab:Hire_Date> <ab:Company>GMS USA</ab:Company> <ab:Location>Office</ab:Location> <ab:Annual_Salary>110011.13</ab:Annual_Salary> </ab:Row> <ab:Row> <ab:Employee_ID>1002</ab:Employee_ID> <ab:Name Type="Full Name">James Tyler</ab:Name> <ab:Hire_Date>2020-12-20</ab:Hire_Date> <ab:Company>GMS USA</ab:Company> <ab:Location>Store</ab:Location> <ab:Annual_Salary>91232</ab:Annual_Salary> </ab:Row> <ab:Row> <ab:Employee_ID>1003</ab:Employee_ID> <ab:Name Type="Full Name">Oliver Parton</ab:Name> <ab:Hire_Date>2020-02-14</ab:Hire_Date> <ab:Company>GMS Germany</ab:Company> <ab:Location>Store</ab:Location> <ab:Annual_Salary>110011.12</ab:Annual_Salary> </ab:Row> <ab:Row> <ab:Employee_ID>1004</ab:Employee_ID> <ab:Name Type="Full Name">Oliver Parton</ab:Name> <ab:Hire_Date>2020-02-14</ab:Hire_Date> <ab:Company>GMS Germany</ab:Company> <ab:Location>Store</ab:Location> <ab:Annual_Salary>110000</ab:Annual_Salary> </ab:Row> </Root>
XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ab="http://www.w3.org/TR/html4/" version="2.0"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:text>Company</xsl:text> <xsl:text>
</xsl:text> <xsl:for-each-group select="Root/ab:Row" group-by="ab:Company"> <xsl:value-of select="ab:Company"/> <xsl:text>
</xsl:text> </xsl:for-each-group> </xsl:template> </xsl:stylesheet>
Output
Company GMS USA GMS Germany
XSLT: Nested For-Each-Group
XML
<?xml version="1.0" encoding="UTF-8"?> <Root xmlns:ab="http://www.w3.org/TR/html4/"> <ab:Row> <ab:Employee_ID>1001</ab:Employee_ID> <ab:Name Type="Full Name">Emma Dylan</ab:Name> <ab:Hire_Date>2019-01-13</ab:Hire_Date> <ab:Company>GMS USA</ab:Company> <ab:Location>Office</ab:Location> <ab:Annual_Salary>110011.13</ab:Annual_Salary> </ab:Row> <ab:Row> <ab:Employee_ID>1002</ab:Employee_ID> <ab:Name Type="Full Name">James Tyler</ab:Name> <ab:Hire_Date>2020-12-20</ab:Hire_Date> <ab:Company>GMS USA</ab:Company> <ab:Location>Store</ab:Location> <ab:Annual_Salary>91232</ab:Annual_Salary> </ab:Row> <ab:Row> <ab:Employee_ID>1003</ab:Employee_ID> <ab:Name Type="Full Name">Oliver Parton</ab:Name> <ab:Hire_Date>2020-02-14</ab:Hire_Date> <ab:Company>GMS Germany</ab:Company> <ab:Location>Store</ab:Location> <ab:Annual_Salary>110011.12</ab:Annual_Salary> </ab:Row> <ab:Row> <ab:Employee_ID>1004</ab:Employee_ID> <ab:Name Type="Full Name">Oliver Parton</ab:Name> <ab:Hire_Date>2020-02-14</ab:Hire_Date> <ab:Company>GMS Germany</ab:Company> <ab:Location>Store</ab:Location> <ab:Annual_Salary>110000</ab:Annual_Salary> </ab:Row> </Root>
XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ab="http://www.w3.org/TR/html4/" version="2.0"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:for-each-group select="Root/ab:Row" group-by="ab:Company"> <xsl:value-of select="ab:Company"/> <xsl:for-each-group select="current-group()" group-by="ab:Location"> <xsl:text>
</xsl:text> <xsl:text>-----</xsl:text> <xsl:value-of select="ab:Location"/> </xsl:for-each-group> <xsl:text>
</xsl:text> </xsl:for-each-group> </xsl:template> </xsl:stylesheet>
Output
GMS USA -----Office -----Store GMS Germany -----Store
XSLT: Count, Sum in For-Each-Group
XML
<?xml version="1.0" encoding="UTF-8"?> <Root xmlns:ab="http://www.w3.org/TR/html4/"> <ab:Row> <ab:Employee_ID>1001</ab:Employee_ID> <ab:Name Type="Full Name">Emma Dylan</ab:Name> <ab:Hire_Date>2019-01-13</ab:Hire_Date> <ab:Company>GMS USA</ab:Company> <ab:Location>Office</ab:Location> <ab:Annual_Salary>110011.13</ab:Annual_Salary> </ab:Row> <ab:Row> <ab:Employee_ID>1002</ab:Employee_ID> <ab:Name Type="Full Name">James Tyler</ab:Name> <ab:Hire_Date>2020-12-20</ab:Hire_Date> <ab:Company>GMS USA</ab:Company> <ab:Location>Store</ab:Location> <ab:Annual_Salary>91232</ab:Annual_Salary> </ab:Row> <ab:Row> <ab:Employee_ID>1003</ab:Employee_ID> <ab:Name Type="Full Name">Oliver Parton</ab:Name> <ab:Hire_Date>2020-02-14</ab:Hire_Date> <ab:Company>GMS Germany</ab:Company> <ab:Location>Store</ab:Location> <ab:Annual_Salary>110011.12</ab:Annual_Salary> </ab:Row> <ab:Row> <ab:Employee_ID>1004</ab:Employee_ID> <ab:Name Type="Full Name">Oliver Parton</ab:Name> <ab:Hire_Date>2020-02-14</ab:Hire_Date> <ab:Company>GMS Germany</ab:Company> <ab:Location>Store</ab:Location> <ab:Annual_Salary>110000</ab:Annual_Salary> </ab:Row> </Root>
XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ab="http://www.w3.org/TR/html4/" version="2.0"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:for-each-group select="Root/ab:Row" group-by="ab:Company"> <xsl:value-of select="ab:Company"/> <xsl:text>
</xsl:text> <xsl:text>***</xsl:text> <xsl:text>,Count of employees in this company: </xsl:text> <xsl:value-of select="count(current-group())"/> <xsl:text>
</xsl:text> <xsl:text>***</xsl:text> <xsl:text>,Sum of salaries in this company: </xsl:text> <xsl:value-of select="sum(current-group()/ab:Annual_Salary)"/> <xsl:text>
</xsl:text> <xsl:text>***</xsl:text> <xsl:text>,Average salary in this company: </xsl:text> <xsl:value-of select="sum(current-group()/ab:Annual_Salary) div count(current-group())"/> <xsl:text>,</xsl:text> <xsl:for-each-group select="current-group()" group-by="ab:Location"> <xsl:text>
</xsl:text> <xsl:text> -----</xsl:text> <xsl:value-of select="ab:Location"/> <xsl:text>
</xsl:text> <xsl:text> ************</xsl:text> <xsl:text>Count of employees in this location: </xsl:text> <xsl:value-of select="count(current-group())"/> <xsl:text>
</xsl:text> <xsl:text> ************</xsl:text> <xsl:text>Sum of salaries in this location: </xsl:text> <xsl:value-of select="sum(current-group()/ab:Annual_Salary)"/> <xsl:text>
</xsl:text> <xsl:text> ************</xsl:text> <xsl:text>Average salary in this location: </xsl:text> <xsl:value-of select="sum(current-group()/ab:Annual_Salary) div count(current-group())"/> <xsl:text>,</xsl:text> </xsl:for-each-group> <xsl:text>
</xsl:text> </xsl:for-each-group> </xsl:template> </xsl:stylesheet>
Output
GMS USA ***,Count of employees in this company: 2 ***,Sum of salaries in this company: 201243.13 ***,Average salary in this company: 100621.565, -----Office ************Count of employees in this location: 1 ************Sum of salaries in this location: 110011.13 ************Average salary in this location: 110011.13, -----Store ************Count of employees in this location: 1 ************Sum of salaries in this location: 91232 ************Average salary in this location: 91232, GMS Germany ***,Count of employees in this company: 2 ***,Sum of salaries in this company: 220011.12 ***,Average salary in this company: 110005.56, -----Store ************Count of employees in this location: 2 ************Sum of salaries in this location: 220011.12 ************Average salary in this location: 110005.56,
Additional Tip: you can open xml file in excel.