Here is my source XML
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount>
</Misc>
<Misc>
<Date>2023-10-09-07:00</Date>
<BonusAmount>900.00</BonusAmount>
</Misc>
</Worker>
<Worker>
<UniqueID>4567</UniqueID>
<ExpenseAmount>1507.00</ExpenseAmount>
<Misc>
<Date>2023-10-06-07:00</Date>
<BonusAmount>322.00</BonusAmount>
</Misc>
<Misc>
<Date>2023-10-08-07:00</Date>
<BonusAmount>800.00</BonusAmount>
</Misc>
<Misc>
<Date>2023-10-09-07:00</Date>
<BonusAmount>410.00</BonusAmount>
</Misc>
</Worker>
</Workers>
Here is what I'm trying to do.
2023-10-10-07:00
2023-10-09-07:00
and 2023-10-06-07:00
2023-10-08-07:00
<Date>
and 4 different
dates are there on my source xml, my expected result should have 4
<Records>
nodes and each node holding unique
<Date>
element corresponding to <Worker>
<Worker>
nodes that do not have any <Date>
elements so any <Worker>
nodes do not have <Date>
should be copied under all <Records>
nodesMy current XSLT. Thanks to michael.hor257k
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Workers">
<Workers>
<xsl:variable name="undated" select="Worker[not(Misc)]"/>
<xsl:variable name="dated" select="Worker[(Misc)]"/>
<xsl:for-each-group select="Worker/Misc" group-by="Date">
<Records>
<xsl:copy-of select="$undated"/>
<xsl:copy-of select="current-group()"/>
</Records>
</xsl:for-each-group>
</Workers>
</xsl:template>
</xsl:stylesheet>
My current result
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount>
</Misc>
</Records>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Misc>
<Date>2023-10-09-07:00</Date>
<BonusAmount>900.00</BonusAmount>
</Misc>
<Misc>
<Date>2023-10-09-07:00</Date>
<BonusAmount>410.00</BonusAmount>
</Misc>
</Records>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Misc>
<Date>2023-10-06-07:00</Date>
<BonusAmount>322.00</BonusAmount>
</Misc>
</Records>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Misc>
<Date>2023-10-08-07:00</Date>
<BonusAmount>800.00</BonusAmount>
</Misc>
</Records>
</Workers>
My current output has 4 <Records>
nodes as expected but <Misc>
--> <Date>
doesn't have it's parent <Worker>
node as below, which is incorrect.
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Misc> <!-- Misc should have been under it's <Worker> node -->
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount>
</Misc>
</Records>
</Workers>
Expected Result
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount>
</Misc>
</Worker>
</Records>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-09-07:00</Date>
<BonusAmount>900.00</BonusAmount>
</Misc>
</Worker>
<Worker>
<UniqueID>4567</UniqueID>
<ExpenseAmount>1507.00</ExpenseAmount>
<Misc>
<Date>2023-10-09-07:00</Date>
<BonusAmount>410.00</BonusAmount>
</Misc>
</Worker>
</Records>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>4567</UniqueID>
<ExpenseAmount>1507.00</ExpenseAmount>
<Misc>
<Date>2023-10-06-07:00</Date>
<BonusAmount>322.00</BonusAmount>
</Misc>
</Worker>
</Records>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>4567</UniqueID>
<ExpenseAmount>1507.00</ExpenseAmount>
<Misc>
<Date>2023-10-08-07:00</Date>
<BonusAmount>800.00</BonusAmount>
</Misc>
</Worker>
</Records>
</Workers>
Can you help me figure out the issue please? Thank you!
======================================================
Only below part is edited
Result after applying XSLT michael.hor257k provided get the expected result based on the source XML originally provided. However, it is possible on my source XML to have more than one <Misc>
node for a same <Date>
but with different <BonusAmount>
under <Worker>
node, like below.
**Source Data**
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount> <!-- Amount is 400 here -->
</Misc>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>600.00</BonusAmount> <!-- Amount is 600 here -->
</Misc>
</Worker>
</Workers>
XSLT produces following result
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount>
</Misc>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>600.00</BonusAmount>
</Misc>
</Worker>
</Records>
</Workers>
Expected Result is below, both <Misc>
elements should be grouped under same <Worker>
node
Any help is appreciated in getting the result as below.
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount>
</Misc>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>600.00</BonusAmount>
</Misc>
</Worker>
</Workers>
Final Solution that gets the expected result is
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Workers">
<Workers>
<xsl:variable name="undated" select="Worker[not(Misc)]"/>
<xsl:for-each-group select="Worker/Misc" group-by="Date">
<Records>
<xsl:copy-of select="$undated"/>
<Worker>
<xsl:copy-of select="../(UniqueID | ExpenseAmount)"/>
</Worker>
<xsl:for-each select="current-group()">
<xsl:copy-of select="."/>
</xsl:for-each>
</Records>
</xsl:for-each-group>
</Workers>
</xsl:template>
</xsl:stylesheet>
Try it this way:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Workers">
<Workers>
<xsl:variable name="undated" select="Worker[not(Misc)]" />
<xsl:for-each-group select="Worker/Misc" group-by="Date">
<Records>
<xsl:copy-of select="$undated"/>
<xsl:for-each select="current-group()">
<Worker>
<xsl:copy-of select="../(UniqueID|ExpenseAmount)"/>
<xsl:copy-of select="."/>
</Worker>
</xsl:for-each>
</Records>
</xsl:for-each-group>
</Workers>
</xsl:template>
</xsl:stylesheet>