Let's learn XSLT through examples. This is the continuation to the previous article Workday XSLT 03- Basic XSLT Examples. I am going to use the XML files given in Workday XSLT 01 - Introduction to XML.
YouTube Video
https://www.youtube.com/watch?v=FWltdLxjzI0
Online XSLT Editor
https://www.freeformatter.com/xsl-transformer.html
XSLT - IF Clause (and/or)
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>100000</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>90000</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>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>Employee ID,Name,Text</xsl:text> <xsl:text>
</xsl:text> <xsl:for-each select="Root/ab:Row"> <xsl:value-of select="ab:Employee_ID"/> <xsl:text>,</xsl:text> <xsl:value-of select="ab:Name"/> <xsl:text>,</xsl:text> <xsl:if test="ab:Company='GMS USA' and ab:Location = 'Office'"> <xsl:text>------- Company is </xsl:text> <xsl:value-of select="ab:Company"/> <xsl:text>; Location is </xsl:text> <xsl:value-of select="ab:Location"/> </xsl:if> <xsl:text>
</xsl:text> </xsl:for-each> </xsl:template> </xsl:stylesheet>
Output
Employee ID,Name,Text 1001,Emma Dylan,------- Company is GMS USA; Location is Office 1002,James Tyler, 1003,Oliver Parton,
XSLT - IF Clause - Working with Numbers and Dates
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>100000</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>90000</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>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>Employee ID,Name,Text</xsl:text> <xsl:text>
</xsl:text> <xsl:for-each select="Root/ab:Row"> <xsl:value-of select="ab:Employee_ID"/> <xsl:text>,</xsl:text> <xsl:value-of select="ab:Name"/> <xsl:text>,</xsl:text> <xsl:if test="ab:Annual_Salary <= 100000"> <xsl:text>------- Annual Salary is </xsl:text> <xsl:value-of select="ab:Annual_Salary"/> <xsl:text>,</xsl:text> </xsl:if> <xsl:if test="ab:Hire_Date >= '2020-01-01'"> <xsl:text>******* Hire Date is </xsl:text> <xsl:value-of select="ab:Hire_Date"/> <xsl:text>,</xsl:text> </xsl:if> <xsl:text>
</xsl:text> </xsl:for-each> </xsl:template> </xsl:stylesheet>
Output
Employee ID,Name,Text 1001,Emma Dylan,------- Annual Salary is 100000, 1002,James Tyler,------- Annual Salary is 90000,******* Hire Date is 2020-12-20, 1003,Oliver Parton,******* Hire Date is 2020-02-14,
XSLT - Choose
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>100000</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>90000</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>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>Employee ID,Name,Text</xsl:text> <xsl:text>
</xsl:text> <xsl:for-each select="Root/ab:Row"> <xsl:value-of select="ab:Employee_ID"/> <xsl:text>,</xsl:text> <xsl:value-of select="ab:Name"/> <xsl:text>,</xsl:text> <xsl:choose> <xsl:when test="ab:Annual_Salary <= 100000"> <xsl:text>------- Annual Salary is less than 100K</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>******* Annual Salary is greater than 100K</xsl:text> </xsl:otherwise> </xsl:choose> <xsl:text>
</xsl:text> </xsl:for-each> </xsl:template> </xsl:stylesheet>
Output
Employee ID,Name,Text 1001,Emma Dylan,------- Annual Salary is greater than 100K 1002,James Tyler,------- Annual Salary is greater than 100K 1003,Oliver Parton,******* Annual Salary is less than 100K