每个父节点下的子节点的XSLT组

时间:2016-07-04 20:45:22

标签: xml xslt grouping

我在查找如何通过for循环中的子节点进行分组时遇到了一些麻烦。起始XML看起来像这样(省略了一些节点):

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

期望的结果是:

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

到目前为止,我有这个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>

问题是所有MarkupTrans_Detail节点都在第一个LAC_SalesOrderId下分组,但我需要它们由每个LAC_SalesOrderId分割。我相信我错过了一些明显的东西,并希望有人可以帮助我解决这个问题:)

1 个答案:

答案 0 :(得分:0)

以这种方式尝试:

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>

应用于您的输入示例,结果将是:

<?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>