I am having some trouble figuring out how to group by child nodes in a for loop like matter. The starting XML looks like this (some nodes are omitted):
<Report>
<LAC_SalesOrderId>
<APL>
<LAC_Customer>09680001</LAC_Customer>
</APL>
<Detail>
<LACRecId_DONOT_CHANGE_Textbox169>5637144820</LACRecId_DONOT_CHANGE_Textbox169>
<LAC_MarkupTransLines>
<MarkupTrans>
<Detail_Collection>
<Detail>
<MarkupTrans_Detail>
<FIELD_MarkupTrans_MarkupCode>Avgift</FIELD_MarkupTrans_MarkupCode>
<FIELD_MarkupTrans_Value>10,00</FIELD_MarkupTrans_Value>
</MarkupTrans_Detail>
</Detail>
<Detail>
<MarkupTrans_Detail>
<FIELD_MarkupTrans_MarkupCode>Frakt</FIELD_MarkupTrans_MarkupCode>
<FIELD_MarkupTrans_Value>10,00</FIELD_MarkupTrans_Value>
</MarkupTrans_Detail>
</Detail>
<Detail>
<MarkupTrans_Detail>
<FIELD_MarkupTrans_MarkupCode>Avgift</FIELD_MarkupTrans_MarkupCode>
<FIELD_MarkupTrans_Value>10,00</FIELD_MarkupTrans_Value>
</MarkupTrans_Detail>
</Detail>
</Detail_Collection>
</MarkupTrans>
</LAC_MarkupTransLines>
</Detail>
</LAC_SalesOrderId>
<LAC_SalesOrderId>
<APL>
<LAC_Customer>09680002</LAC_Customer>
</APL>
<Detail>
<LACRecId_DONOT_CHANGE_Textbox169>5637144821</LACRecId_DONOT_CHANGE_Textbox169>
<LAC_MarkupTransLines>
<MarkupTrans>
<Detail_Collection>
<Detail>
<MarkupTrans_Detail>
<FIELD_MarkupTrans_MarkupCode>Avgift</FIELD_MarkupTrans_MarkupCode>
<FIELD_MarkupTrans_Value>20,00</FIELD_MarkupTrans_Value>
</MarkupTrans_Detail>
</Detail>
<Detail>
<MarkupTrans_Detail>
<FIELD_MarkupTrans_MarkupCode>Frakt</FIELD_MarkupTrans_MarkupCode>
<FIELD_MarkupTrans_Value>30,00</FIELD_MarkupTrans_Value>
</MarkupTrans_Detail>
</Detail>
<Detail>
<MarkupTrans_Detail>
<FIELD_MarkupTrans_MarkupCode>Avgift</FIELD_MarkupTrans_MarkupCode>
<FIELD_MarkupTrans_Value>20,00</FIELD_MarkupTrans_Value>
</MarkupTrans_Detail>
</Detail>
</Detail_Collection>
</MarkupTrans>
</LAC_MarkupTransLines>
</Detail>
</LAC_SalesOrderId>
</Report>
The desired result would be:
<Report>
<LAC_SalesOrderId>
<APL>
<LAC_Customer>09680001</LAC_Customer>
</APL>
<Detail>
<LACRecId_DONOT_CHANGE_Textbox169>5637144820</LACRecId_DONOT_CHANGE_Textbox169>
<LAC_MarkupTransLines>
<MarkupTrans>
<Detail_Collection>
<MarkupCode>
<Code>Avgift</Code>
<MarkupTrans_Detail>
<FIELD_MarkupTrans_MarkupCode>Avgift</FIELD_MarkupTrans_MarkupCode>
<FIELD_MarkupTrans_Value>10,00</FIELD_MarkupTrans_Value>
</MarkupTrans_Detail>
<MarkupTrans_Detail>
<FIELD_MarkupTrans_MarkupCode>Avgift</FIELD_MarkupTrans_MarkupCode>
<FIELD_MarkupTrans_Value>10,00</FIELD_MarkupTrans_Value>
</MarkupTrans_Detail>
</MarkupCode>
<MarkupCode>
<Code>Frakt</Code>
<MarkupTrans_Detail>
<FIELD_MarkupTrans_MarkupCode>Frakt</FIELD_MarkupTrans_MarkupCode>
<FIELD_MarkupTrans_Value>10,00</FIELD_MarkupTrans_Value>
</MarkupTrans_Detail>
</MarkupCode>
</Detail_Collection>
</MarkupTrans>
</LAC_MarkupTransLines>
</Detail>
</LAC_SalesOrderId>
<LAC_SalesOrderId>
<APL>
<LAC_Customer>09680002</LAC_Customer>
</APL>
<Detail>
<LACRecId_DONOT_CHANGE_Textbox169>5637144821</LACRecId_DONOT_CHANGE_Textbox169>
<LAC_MarkupTransLines>
<MarkupTrans>
<Detail_Collection>
<MarkupCode>
<Code>Avgift</Code>
<MarkupTrans_Detail>
<FIELD_MarkupTrans_MarkupCode>Avgift</FIELD_MarkupTrans_MarkupCode>
<FIELD_MarkupTrans_Value>20,00</FIELD_MarkupTrans_Value>
</MarkupTrans_Detail>
<MarkupTrans_Detail>
<FIELD_MarkupTrans_MarkupCode>Avgift</FIELD_MarkupTrans_MarkupCode>
<FIELD_MarkupTrans_Value>20,00</FIELD_MarkupTrans_Value>
</MarkupTrans_Detail>
</MarkupCode>
<MarkupCode>
<Code>Frakt</Code>
<MarkupTrans_Detail>
<FIELD_MarkupTrans_MarkupCode>Frakt</FIELD_MarkupTrans_MarkupCode>
<FIELD_MarkupTrans_Value>30,00</FIELD_MarkupTrans_Value>
</MarkupTrans_Detail>
</MarkupCode>
</Detail_Collection>
</MarkupTrans>
</LAC_MarkupTransLines>
</Detail>
</LAC_SalesOrderId>
</Report>
Up until now I have this XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="groups" match="//FIELD_MarkupTrans_MarkupCode" use="."/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//LAC_MarkupTransLines/MarkupTrans/Detail_Collection">
<Detail_Collection>
<xsl:apply-templates select="Detail/MarkupTrans_Detail/FIELD_MarkupTrans_MarkupCode[generate-id() = generate-id(key('groups', .)[1])]"/>
</Detail_Collection>
</xsl:template>
<xsl:template match="FIELD_MarkupTrans_MarkupCode">
<xsl:variable name="currentGroup" select="."/>
<MarkupCode>
<xsl:copy-of select="../FIELD_MarkupTrans_MarkupCode"/>
<xsl:for-each select="key('groups', $currentGroup)">
<xsl:copy-of select="../.."/>
</xsl:for-each>
</MarkupCode>
</xsl:template>
</xsl:stylesheet>
The issue is that all the MarkupTrans_Detail nodes are grouped under the first LAC_SalesOrderId but I need them to be splitted by each LAC_SalesOrderId. I believe that I am missing something obvious and hope that there is someone that can help me figure it out :)