Workday XSLT 03- Basic XSLT Examples

Workday XSLT 03- Basic XSLT Examples

Let's learn XSLT through examples. This is the continuation to the previous article Workday XSLT 02- XSLT Examples. I am going to use the XML files given in Workday XSLT 01 - Introduction to XML.

YouTube Video

youtube.com/watch?v=d6lWCaqPPYQ

Online XSLT Editor

freeformatter.com/xsl-transformer.html

XSLT - Access Attributes

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>2020-01-13</ab:Hire_Date>
        <ab:Company>GMS USA</ab:Company>
        <ab:Dependent Type="Spouse">
            <ab:Dependent_ID>01</ab:Dependent_ID>
            <ab:Dependent_Name>Benjamin Dylan</ab:Dependent_Name>
        </ab:Dependent>
        <ab:Dependent Type="Child">
            <ab:Dependent_ID>02</ab:Dependent_ID>
            <ab:Dependent_Name>Henry Dylan</ab:Dependent_Name>
        </ab:Dependent>
    </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 UK</ab:Company>
    </ab:Row>
    <ab:Row>
        <ab:Employee_ID>1003</ab:Employee_ID>
        <ab:Name Type="Full Name">Oliver Parton</ab:Name>
        <ab:Hire_Date>2019-02-14</ab:Hire_Date>
        <ab:Company>GMS Germany</ab:Company>
    </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,Dependent Name</xsl:text>
        <xsl:text>&#xa;</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:value-of select="ab:Name/@Type" />

            <xsl:text>&#xa;</xsl:text>
            <xsl:for-each select="ab:Dependent">
                <xsl:text>-----------------</xsl:text>
                <xsl:value-of select="@Type" />
                <xsl:text>,</xsl:text>
                <xsl:value-of select="ab:Dependent_Name" />
                <xsl:text>&#xa;</xsl:text>
            </xsl:for-each>
        </xsl:for-each>


    </xsl:template>

</xsl:stylesheet>

Output

Employee ID,Name,Dependent Name
1001,Emma Dylan,Full Name
-----------------Spouse,Benjamin Dylan
-----------------Child,Henry Dylan
1002,James Tyler,Full Name
1003,Oliver Parton,Full Name

XSLT - Condition based on Attributes

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>2020-01-13</ab:Hire_Date>
        <ab:Company>GMS USA</ab:Company>
        <ab:Dependent Type="Spouse">
            <ab:Dependent_ID>01</ab:Dependent_ID>
            <ab:Dependent_Name>Benjamin Dylan</ab:Dependent_Name>
        </ab:Dependent>
        <ab:Dependent Type="Child">
            <ab:Dependent_ID>02</ab:Dependent_ID>
            <ab:Dependent_Name>Henry Dylan</ab:Dependent_Name>
        </ab:Dependent>
    </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 UK</ab:Company>
    </ab:Row>
    <ab:Row>
        <ab:Employee_ID>1003</ab:Employee_ID>
        <ab:Name Type="Full Name">Oliver Parton</ab:Name>
        <ab:Hire_Date>2019-02-14</ab:Hire_Date>
        <ab:Company>GMS Germany</ab:Company>
    </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,Dependent Name</xsl:text>
        <xsl:text>&#xa;</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:value-of select="ab:Name/@Type" />

            <xsl:text>&#xa;</xsl:text>
            <xsl:for-each select="ab:Dependent[@Type = 'Spouse']">
                <xsl:text>-----------------</xsl:text>
                <xsl:value-of select="@Type" />
                <xsl:text>,</xsl:text>
                <xsl:value-of select="ab:Dependent_Name" />
                <xsl:text>&#xa;</xsl:text>
            </xsl:for-each>
        </xsl:for-each>


    </xsl:template>

</xsl:stylesheet>

Output

Employee ID,Name,Dependent Name
1001,Emma Dylan,Full Name
-----------------Spouse,Benjamin Dylan
1002,James Tyler,Full Name
1003,Oliver Parton,Full Name

XSLT - for Workday generated XML

XML

<?xml version='1.0' encoding='UTF-8'?>
<wd:Report_Data xmlns:wd="urn:com.workday.report/INT001_-_Sample_Report_-_Ujwal">
   <wd:Report_Entry>
      <wd:Worker>Logan McNeil</wd:Worker>
      <wd:Employee_ID>21001</wd:Employee_ID>
      <wd:Hire_Date>2000-01-01</wd:Hire_Date>
      <wd:Dependents_group>
         <wd:Dependent>Megan McNeil</wd:Dependent>
         <wd:Relationship>Child</wd:Relationship>
      </wd:Dependents_group>
      <wd:Dependents_group>
         <wd:Dependent>Pat McNeil</wd:Dependent>
         <wd:Relationship>Spouse</wd:Relationship>
      </wd:Dependents_group>
   </wd:Report_Entry>
   <wd:Report_Entry>
      <wd:Worker>John Davis</wd:Worker>
      <wd:Employee_ID>21505</wd:Employee_ID>
      <wd:Hire_Date>2022-08-29</wd:Hire_Date>
   </wd:Report_Entry>
</wd:Report_Data>

XSLT

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:wd="urn:com.workday.report/INT001_-_Sample_Report_-_Ujwal"
    version="2.0">
    <xsl:output method="text" />

    <xsl:template match="/">

        <xsl:text>Employee ID,Name</xsl:text>
        <xsl:text>&#xa;</xsl:text>

        <xsl:for-each select="wd:Report_Data/wd:Report_Entry">
            <xsl:value-of select="wd:Employee_ID" />
            <xsl:text>,</xsl:text>
            <xsl:value-of select="wd:Worker" />

            <xsl:text>&#xa;</xsl:text>

        </xsl:for-each>


    </xsl:template>

</xsl:stylesheet>

Output

Employee ID,Name
21001,Logan McNeil
21505,John Davis