Workday XSLT 05- Basic XSLT Examples (Traverse Up and Down)

Workday XSLT 05- Basic XSLT Examples (Traverse Up and Down)

Let's learn XSLT through examples. This is the continuation of the previous article Workday XSLT 04- Basic XSLT Examples (If, Choose).

YouTube Video

Online XSLT Editor

https://www.freeformatter.com/xsl-transformer.html

XSLT - Traverse up node - Example 1

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:Dependent Type="Child">
            <ab:Dependent_ID>03</ab:Dependent_ID>
            <ab:Dependent_Name>Erin 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>&#xa;</xsl:text>
            <xsl:for-each select="ab:Dependent">

                <xsl:text>-----------------</xsl:text>
                <xsl:value-of select="ab:Dependent_Name"/>
                <xsl:text>,</xsl:text>
                <xsl:value-of select="../ab:Employee_ID"/>
                <xsl:text>&#xa;</xsl:text>    
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

Output

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

XSLT - Traverse up node - Example 2

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:spouse>
            <ab:Dependent Type="Spouse">
                <ab:Dependent_ID>01</ab:Dependent_ID>
                <ab:Dependent_Name>Benjamin Dylan</ab:Dependent_Name>
            </ab:Dependent>
        </ab:spouse>
        <ab:chidlren>
            <ab:Dependent Type="Child">
                <ab:Dependent_ID>02</ab:Dependent_ID>
                <ab:Dependent_Name>Henry Dylan</ab:Dependent_Name>
            </ab:Dependent>
            <ab:Dependent Type="Child">
                <ab:Dependent_ID>03</ab:Dependent_ID>
                <ab:Dependent_Name>Erin Dylan</ab:Dependent_Name>
            </ab:Dependent>
        </ab:chidlren>
    </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>&#xa;</xsl:text>
            <xsl:for-each select="ab:chidlren/ab:Dependent">

                <xsl:text>-----------------</xsl:text>
                <xsl:value-of select="ab:Dependent_Name"/>
                <xsl:text>,</xsl:text>
                <xsl:value-of select="../../ab:Employee_ID"/>
                <xsl:text>&#xa;</xsl:text>    
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

Output

Employee ID,Name,Dependent Name
1001,Emma Dylan
-----------------Henry Dylan,1001
-----------------Erin Dylan,1001
1002,James Tyler
1003,Oliver Parton

XSLT - Traverse down node (without for-each) the first node, n th node, last node

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:spouse>
            <ab:Dependent Type="Spouse">
                <ab:Dependent_ID>01</ab:Dependent_ID>
                <ab:Dependent_Name>Benjamin Dylan</ab:Dependent_Name>
            </ab:Dependent>
        </ab:spouse>
        <ab:chidlren>
            <ab:Dependent Type="Child">
                <ab:Dependent_ID>02</ab:Dependent_ID>
                <ab:Dependent_Name>Henry Dylan</ab:Dependent_Name>
            </ab:Dependent>
            <ab:Dependent Type="Child">
                <ab:Dependent_ID>03</ab:Dependent_ID>
                <ab:Dependent_Name>Erin Dylan</ab:Dependent_Name>
            </ab:Dependent>
        </ab:chidlren>
    </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="position()"/>
            <xsl:text>&#xa;</xsl:text>
                <xsl:text>-----------------Spouse Information: </xsl:text>
                <xsl:value-of select="ab:spouse/ab:Dependent/ab:Dependent_Name"/>
                <xsl:text>&#xa;</xsl:text>

                <xsl:text>-----------------Children Information: </xsl:text>
                <xsl:value-of select="ab:chidlren/ab:Dependent/ab:Dependent_Name"/>
                <xsl:text>&#xa;</xsl:text>

                <xsl:text>-----------------First Children Information: </xsl:text>
                <xsl:value-of select="ab:chidlren/ab:Dependent[1]/ab:Dependent_Name"/>
                <xsl:text>&#xa;</xsl:text>

                <xsl:text>-----------------Second Children Information: </xsl:text>
                <xsl:value-of select="ab:chidlren/ab:Dependent[2]/ab:Dependent_Name"/>
                <xsl:text>&#xa;</xsl:text>

                <xsl:text>-----------------Third Children Information: </xsl:text>
                <xsl:value-of select="ab:chidlren/ab:Dependent[3]/ab:Dependent_Name"/>
                <xsl:text>&#xa;</xsl:text>

                <xsl:text>-----------------First Children Information (Alternative Approach 1): </xsl:text>
                <xsl:value-of select="ab:chidlren/ab:Dependent[position()=1]/ab:Dependent_Name"/>
                <xsl:text>&#xa;</xsl:text>

                <xsl:text>-----------------Second Children Information (Alternative Approach 1): </xsl:text>
                <xsl:value-of select="ab:chidlren/ab:Dependent[position()=2]/ab:Dependent_Name"/>
                <xsl:text>&#xa;</xsl:text>


                <xsl:text>-----------------Last Children Information: </xsl:text>
                <xsl:value-of select="ab:chidlren/ab:Dependent[position()=last()]/ab:Dependent_Name"/>
                <xsl:text>&#xa;</xsl:text>

                <xsl:text>-----------------Last but one Children Information: </xsl:text>
                <xsl:value-of select="ab:chidlren/ab:Dependent[position()=last()-1]/ab:Dependent_Name"/>
                <xsl:text>&#xa;</xsl:text>

                <xsl:text>-----------------All Children Information greater than 1: </xsl:text>
                <xsl:value-of select="ab:chidlren/ab:Dependent[position() &gt; 1]/ab:Dependent_Name"/>
                <xsl:text>&#xa;</xsl:text>

        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

Output

Employee ID,Name,Dependent Name
1001,Emma Dylan,1
-----------------Spouse Information: Benjamin Dylan
-----------------Children Information: Henry Dylan Erin Dylan Tyler Dylan
-----------------First Children Information: Henry Dylan
-----------------Second Children Information: Erin Dylan
-----------------Third Children Information: Tyler Dylan
-----------------First Children Information (Alternative Approach 1): Henry Dylan
-----------------Second Children Information (Alternative Approach 1): Erin Dylan
-----------------Last Children Information: Tyler Dylan
-----------------Last but one Children Information: Erin Dylan
-----------------All Children Information greater than 1: Erin Dylan Tyler Dylan
1002,James Tyler,2
-----------------Spouse Information: 
-----------------Children Information: 
-----------------First Children Information: 
-----------------Second Children Information: 
-----------------Third Children Information: 
-----------------First Children Information (Alternative Approach 1): 
-----------------Second Children Information (Alternative Approach 1): 
-----------------Last Children Information: 
-----------------Last but one Children Information: 
-----------------All Children Information greater than 1: 
1003,Oliver Parton,3
-----------------Spouse Information: 
-----------------Children Information: 
-----------------First Children Information: 
-----------------Second Children Information: 
-----------------Third Children Information: 
-----------------First Children Information (Alternative Approach 1): 
-----------------Second Children Information (Alternative Approach 1): 
-----------------Last Children Information: 
-----------------Last but one Children Information: 
-----------------All Children Information greater than 1:

XSLT - preceding-sibling; following-sibling

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: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,Text</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>&#xa;</xsl:text>

            <xsl:text>,-------------</xsl:text>
            <xsl:text>First preceding-sibling: </xsl:text>
            <xsl:value-of select="preceding-sibling::ab:Row[1]/ab:Employee_ID"/>
            <xsl:text>&#xa;</xsl:text>
            <xsl:text>,-------------</xsl:text>
            <xsl:text>Second preceding-sibling: </xsl:text>
            <xsl:value-of select="preceding-sibling::ab:Row[2]/ab:Employee_ID"/>
            <xsl:text>&#xa;</xsl:text>

            <xsl:text>,-------------</xsl:text>
            <xsl:text>Count of preceding-siblings: </xsl:text>
            <xsl:value-of select="count(preceding-sibling::*)"/>
            <xsl:text>&#xa;</xsl:text>

            <xsl:text>,-------------</xsl:text>
            <xsl:text>First following-sibling: </xsl:text>
            <xsl:value-of select="following-sibling::ab:Row[1]/ab:Employee_ID"/>
            <xsl:text>&#xa;</xsl:text>
            <xsl:text>,-------------</xsl:text>
            <xsl:text>Second following-sibling: </xsl:text>
            <xsl:value-of select="following-sibling::ab:Row[2]/ab:Employee_ID"/>
            <xsl:text>&#xa;</xsl:text>

            <xsl:text>,-------------</xsl:text>
            <xsl:text>Count of following-siblings: </xsl:text>
            <xsl:value-of select="count(following-sibling::*)"/>

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

        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

Output

Employee ID,Name,Text
1001,Emma Dylan
,-------------First preceding-sibling: 
,-------------Second preceding-sibling: 
,-------------Count of preceding-siblings: 0
,-------------First following-sibling: 1002
,-------------Second following-sibling: 1003
,-------------Count of following-siblings: 2
1002,James Tyler
,-------------First preceding-sibling: 1001
,-------------Second preceding-sibling: 
,-------------Count of preceding-siblings: 1
,-------------First following-sibling: 1003
,-------------Second following-sibling: 
,-------------Count of following-siblings: 1
1003,Oliver Parton
,-------------First preceding-sibling: 1002
,-------------Second preceding-sibling: 1001
,-------------Count of preceding-siblings: 2
,-------------First following-sibling: 
,-------------Second following-sibling: 
,-------------Count of following-siblings: 0