Search code examples
xmlxsltxslt-2.0

Group transformed elements to its parent


I have XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
xmlns:pi="urn:com.workday/picof"
version="2.0">

<xsl:output indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <pi:Payroll_Extract_Employees>
        <xsl:copy-of select="//pi:Header"/>
        <xsl:for-each select="//pi:Employee">
        <pi:Employee>
            <xsl:copy-of select="pi:Summary"/>
        </pi:Employee>
        </xsl:for-each>
            <xsl:apply-templates select="//pi:Employee/pi:Additional_Information/*"/>
    </pi:Payroll_Extract_Employees>     
</xsl:template>

<xsl:template match="pi:Additional_Information/*">
    <xsl:apply-templates select="ancestor::pi:Payroll_Extract_Employees">
        <xsl:with-param name="UnpaidTO" select="current()"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="pi:Payroll_Extract_Employees">
    <xsl:param name="UnpaidTO"/>

    <pi:Time_Off>
        <pi:Code_Name>Unpaid Time Off</pi:Code_Name>
        <pi:Time_Off_Type><xsl:value-of select="substring-after(substring-before($UnpaidTO,'('),'=')"/></pi:Time_Off_Type>
        <pi:Time_Off_Date><xsl:value-of select="substring-before($UnpaidTO,',')"/></pi:Time_Off_Date>
        <pi:Quantity><xsl:value-of select="substring-before(substring-after($UnpaidTO,','),' ')"/></pi:Quantity>
        <pi:Unit_of_Time><xsl:value-of select="substring-after(substring-before($UnpaidTO,'='),' ')"/></pi:Unit_of_Time>
    </pi:Time_Off>

  </xsl:template>

  </xsl:stylesheet>

And an initial XML:

<?xml version="1.0" encoding="UTF-8"?>
<pi:Payroll_Extract_Employees xmlns:pi="urn:com.workday/picof">
    <pi:Header>
        <pi:Updated_From>2017-09-07T02:23:04.000-07:00</pi:Updated_From>
    </pi:Header>
    <pi:Employee>
        <pi:Summary>
            <pi:Employee_ID>00000001</pi:Employee_ID>
        </pi:Summary>
        <pi:Additional_Information>
            <pi:Strike_1>2017-09-11,2 Hours=9570</pi:Strike_1>
        </pi:Additional_Information>
    </pi:Employee>
    <pi:Employee>
        <pi:Summary>
            <pi:Employee_ID>00000002</pi:Employee_ID>
        </pi:Summary>
        <pi:Additional_Information>
            <pi:Strike_1>2017-09-22,8 Hours=9570</pi:Strike_1>
            <pi:Unjustified_Absence_1>2017-09-25,8 Hours=9700</pi:Unjustified_Absence_1>
        </pi:Additional_Information>
    </pi:Employee>
</pi:Payroll_Extract_Employees>

My code is working well. The only problem is that the transformed elements (pi:Time_Off and its child elements) are not being group to their respective parent node (pi:Employee)

I would like the final result to be like this one:

<?xml version="1.0" encoding="UTF-8"?>
<pi:Payroll_Extract_Employees xmlns:pi="urn:com.workday/picof">
    <pi:Header>
        <pi:Updated_From>2017-09-07T02:23:04.000-07:00</pi:Updated_From>
    </pi:Header>
    <pi:Employee>
        <pi:Summary>
            <pi:Employee_ID>00000001</pi:Employee_ID>
        </pi:Summary>
        <pi:Time_Off>
         <pi:Code_Name>Unpaid Time Off</pi:Code_Name>
         <pi:Time_Off_Type>9570</pi:Time_Off_Type>
         <pi:Time_Off_Date>2017-09-11</pi:Time_Off_Date>
         <pi:Quantity>2</pi:Quantity>
         <pi:Unit_of_Time>Hours</pi:Unit_of_Time>
        </pi:Time_Off>
    </pi:Employee>
    <pi:Employee>
        <pi:Summary>
           <pi:Employee_ID>00000002</pi:Employee_ID>
        </pi:Summary>
        <pi:Time_Off>
           <pi:Code_Name>Unpaid Time Off</pi:Code_Name>
           <pi:Time_Off_Type>9570</pi:Time_Off_Type>
           <pi:Time_Off_Date>2017-09-22</pi:Time_Off_Date>
           <pi:Quantity>8</pi:Quantity>
           <pi:Unit_of_Time>Hours</pi:Unit_of_Time>
        </pi:Time_Off>
        <pi:Time_Off>
           <pi:Code_Name>Unpaid Time Off</pi:Code_Name>
           <pi:Time_Off_Type>9700</pi:Time_Off_Type>
           <pi:Time_Off_Date>2017-09-25</pi:Time_Off_Date>
           <pi:Quantity>8</pi:Quantity>
           <pi:Unit_of_Time>Hours</pi:Unit_of_Time>
        </pi:Time_Off>
    </pi:Employee>
  </pi:Payroll_Extract_Employees>

What should I do to have make it like the desired result? Kindly help me to resolve this one. Thank you so much!


Solution

  • The problem is with this statement...

    <xsl:apply-templates select="//pi:Employee/pi:Additional_Information/*"/>
    

    Firstly it is outside the xsl:for-each on Employees, when it should be inside it (inside the creation of the pi:Employee element).

    Secondly, the // at the start means it is selecting all Employees, when really you just want the expression relative to the current employee (i.e select the child Additional_Information elements).

    Try this template instead

    <xsl:template match="/">
        <pi:Payroll_Extract_Employees>
            <xsl:copy-of select="//pi:Header"/>
            <xsl:for-each select="//pi:Employee">
            <pi:Employee>
                <xsl:copy-of select="pi:Summary"/>
                <xsl:apply-templates select="pi:Additional_Information/*"/>
            </pi:Employee>
            </xsl:for-each>
        </pi:Payroll_Extract_Employees>     
    </xsl:template>