0
votes

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 :)

1

1 Answers

0
votes

Try it this way:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="grp" match="MarkupTrans_Detail" use="concat(FIELD_MarkupTrans_MarkupCode, '|', generate-id(ancestor::LAC_SalesOrderId))"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Detail_Collection">
    <xsl:copy>
        <xsl:for-each select="Detail//MarkupTrans_Detail[generate-id() = generate-id(key('grp', concat(FIELD_MarkupTrans_MarkupCode, '|', generate-id(ancestor::LAC_SalesOrderId)))[1])]">
            <MarkupCode>
                <Code>
                    <xsl:value-of select="FIELD_MarkupTrans_MarkupCode"/>
                </Code>
                <xsl:apply-templates select="key('grp', concat(FIELD_MarkupTrans_MarkupCode, '|', generate-id(ancestor::LAC_SalesOrderId)))"/>                      
            </MarkupCode>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Applied to your input example, the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<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>