Skip to Content
avatar image
Former Member

XSLT mapping - split in individual records from complex input xml

Hi Everybody,

I want to split up individual records from a complex xml output by XSLT mapping.

Below is input xml:

<EmployeeTime> <EmployeeTime> <userId>100</userId> <timeType>A</timeType> <timeCalendar> <EmployeeTimeCalendar> <date>2018-05-10</date> <quantityInHours>8</quantityInHours> </EmployeeTimeCalendar> </timeCalendar> </EmployeeTime> <EmployeeTime> <userId>110</userId> <timeType>B</timeType> <timeCalendar> <EmployeeTimeCalendar> <date>2018-06-05</date> <quantityInHours>8</quantityInHours> </EmployeeTimeCalendar> <EmployeeTimeCalendar> <date>2018-06-06</date> <quantityInHours>8</quantityInHours> </EmployeeTimeCalendar> </timeCalendar> </EmployeeTime> </EmployeeTime>

Output should be like:

<EmployeeTime> <EmployeeTime> <userId>100</userId> <timeType>A</timeType> <timeCalendar> <EmployeeTimeCalendar> <date>2018-05-10</date> <quantityInHours>8</quantityInHours> </EmployeeTimeCalendar> </timeCalendar> </EmployeeTime> <EmployeeTime> <userId>110</userId> <timeType>B</timeType> <timeCalendar> <EmployeeTimeCalendar> <date>2018-06-05</date> <quantityInHours>8</quantityInHours> </EmployeeTimeCalendar> </timeCalendar> </EmployeeTime> <EmployeeTime> <userId>110</userId> <timeType>B</timeType> <timeCalendar> <EmployeeTimeCalendar> <date>2018-06-06</date> <quantityInHours>8</quantityInHours> </EmployeeTimeCalendar> </timeCalendar> </EmployeeTime> </EmployeeTime>

Can you please advise how to achieve this in XSLT.

Regards,

Matt

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    May 18 at 09:55 PM

    Hi Matt

    Great question! The following solution works, but it's not that elegant, I'm afraid. In a nutshell, it iterates over each EmployeeTimeCalender element, and copies its parent. Here's the stylesheet:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:template match="/">
            <EmployeeTime>
                <xsl:for-each select="//EmployeeTimeCalendar">
                    <xsl:variable name="parent" select="./ancestor::EmployeeTime[1]"/>
                    <EmployeeTime>
                        <xsl:copy-of select="$parent/userId"/>
                        <xsl:copy-of select="$parent/timeType"/>
                        <timeCalendar>
                            <xsl:copy-of select="."/>
                        </timeCalendar>
                    </EmployeeTime>
                </xsl:for-each>
            </EmployeeTime>
        </xsl:template>
    
    </xsl:stylesheet>

    There's a nicer solution, I'm sure, but in my defense, it is quite late :-)

    Regards,

    Morten

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member
      Hi Morten,

      Thanks for helping. My Purpose was to chunk out EmployeeTimeCalender elements individually before passing them through another mapping.

      I've tested it out & it worked great !! So, I can only say it would suffice my purpose perfectly.

      Again, a many thanks for the help.

      Regards,

      Matt