cancel
Showing results for 
Search instead for 
Did you mean: 

XSLT mapping - split in individual records from complex input xml

0 Kudos

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

MortenWittrock
Active Contributor
0 Kudos

By the way, Matt: Pretty-printing the XML examples and placing them in code tags would make your question much easier to read.

Regards,

Morten

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
Active Contributor
0 Kudos

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

0 Kudos
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

Answers (0)