on 05-18-2018 8:31 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.