我无法理解 XSLT 1.0 Muenchian 分组

I cannot get my head around XSLT 1.0 Muenchian grouping

我基本上只是尝试按容器 ID 对 DCLDET 段进行分组,因此我可以创建一个额外的标签

<Carton>
    <Container_Id>
    <SKU>
    <Qty_Shipped>
</Carton>

我已经阅读了有关 Muenchian 分组的内容,但我所做的一切似乎都无济于事。我想如果我按容器 ID 在 DCL/DCLDET 段上分组,这就是我需要的吗?但我得到的只是没有结构的输出代码(如果需要,可以 post 这个,但它本质上只是没有标签的文本)。

我的XML是这个-

        <?xml version="1.0" encoding="UTF-8"?>
<DSV_DESADV0103>
   <HEAD>
      <Message_Id>260719123738792107</Message_Id>
      <Date_Time>20190726123738</Date_Time>
      <Message_Type>DESADV</Message_Type>
      <Message_Version>0103</Message_Version>
      <Sender_Id>DSV_WMS_RP01</Sender_Id>
      <Sender_Company>DSV Solutions</Sender_Company>
      <Recipient_Id>DECLARKS</Recipient_Id>
      <Recipient_Company>Clarks</Recipient_Company>
      <Common_Acces_Reference>Ready to Load</Common_Acces_Reference>
      <DCH>
         <Site_Id>DEKRE01</Site_Id>
         <Client_Id>DECLARKS</Client_Id>
         <Order_Id>1013791824</Order_Id>
         <Order_Type>WHOLESALE</Order_Type>
         <Order_Weight>38.089</Order_Weight>
         <Work_Group>1013791824</Work_Group>
         <Order_Date>20190726104712</Order_Date>
         <Order_Volume>.298032</Order_Volume>
         <Owner_Id>DECLARKSFP</Owner_Id>
         <Consignment>20190725</Consignment>
         <Carrier_Id>TNTDE</Carrier_Id>
         <Ship_By_Date>20190725000000</Ship_By_Date>
         <Shipped_Date>20190726123739</Shipped_Date>
         <Deliver_By_Date>20190725000000</Deliver_By_Date>
         <Service_Level>OL PARCEL ECON EXPR</Service_Level>
         <Purchase_Order>20190725TNTDE106229</Purchase_Order>
         <Export>N</Export>
         <Load_Sequence>10</Load_Sequence>
         <Order_Value>0</Order_Value>
         <COD_Flag>N</COD_Flag>
         <Order_Reference>SFS60627202450</Order_Reference>
         <Status>Ready to Load</Status>
         <DCHPTY>
            <Party_Role>CN</Party_Role>
            <Party_Id>106229</Party_Id>
            <Party_Name>Majo
Schuhe</Party_Name>
            <Party_Address_1>Margarete-von-Wrangellstr. 2</Party_Address_1>
            <Town>KELKHEIM</Town>
            <Postcode>65779</Postcode>
            <Country_Code>DE</Country_Code>
            <CC3>DEU</CC3>
            <Contact_Phone>06195-976500</Contact_Phone>
            <Contact_Email>rechnungen@majo-markenschuhe.de</Contact_Email>
         </DCHPTY>
         <DCHPTY>
            <Party_Role>IV</Party_Role>
            <Party_Id>106229</Party_Id>
            <Party_Name>Majo
Schuhe</Party_Name>
            <Town>KELKHEIM</Town>
            <Postcode>65779</Postcode>
            <Country_Code>DE</Country_Code>
            <CC3>DEU</CC3>
         </DCHPTY>
         <DCHUDF>
            <User_Def_Type_1>BOX</User_Def_Type_1>
            <User_Def_Type_2>M</User_Def_Type_2>
            <User_Def_Type_3>Y</User_Def_Type_3>
            <User_Def_Type_4>DC02</User_Def_Type_4>
            <User_Def_Type_5>91</User_Def_Type_5>
            <User_Def_Type_6>P</User_Def_Type_6>
            <User_Def_Type_7>0015316096</User_Def_Type_7>
            <User_Def_Type_8>TEU</User_Def_Type_8>
            <User_Def_Num_1>28</User_Def_Num_1>
         </DCHUDF>
         <DCL>
            <Line_Id>10</Line_Id>
            <Sku_Id>261448927060</Sku_Id>
            <Qty_Ordered>1</Qty_Ordered>
            <Qty_Shipped>1</Qty_Shipped>
            <Tracking_Level>BOX</Tracking_Level>
            <Config_Id>261448927060P</Config_Id>
            <Condition_Id>OK1</Condition_Id>
            <Host_Order_Id>001531609</Host_Order_Id>
            <Host_Line_Id>00135</Host_Line_Id>
            <Purchase_Order>SFS60627202450</Purchase_Order>
            <Product_Price_Currency>EUR</Product_Price_Currency>
            <Owner_Id>DECLARKSFP</Owner_Id>
            <Unallocatable>N</Unallocatable>
            <DCLUDF />
            <DCLDET>
               <Tag_Id>90000371169</Tag_Id>
               <Config_Id>261448927060P</Config_Id>
               <Qty_Shipped>1</Qty_Shipped>
               <Catch_Weight>0</Catch_Weight>
               <Condition_Id>OK1</Condition_Id>
               <Ce_Under_Bond>N</Ce_Under_Bond>
               <Pallet_Id>CFPP0287495</Pallet_Id>
               <Container_Id>257084662521524253</Container_Id>
               <Carrier_Container_Id>5355642</Carrier_Container_Id>
            </DCLDET>
         </DCL>
         <DCL>
            <Line_Id>20</Line_Id>
            <Sku_Id>261448927065</Sku_Id>
            <Qty_Ordered>1</Qty_Ordered>
            <Qty_Shipped>1</Qty_Shipped>
            <Tracking_Level>BOX</Tracking_Level>
            <Config_Id>261448927065P</Config_Id>
            <Condition_Id>OK1</Condition_Id>
            <Host_Order_Id>001531609</Host_Order_Id>
            <Host_Line_Id>00136</Host_Line_Id>
            <Purchase_Order>SFS60627202450</Purchase_Order>
            <Product_Price_Currency>EUR</Product_Price_Currency>
            <Owner_Id>DECLARKSFP</Owner_Id>
            <Unallocatable>N</Unallocatable>
            <DCLUDF />
            <DCLDET>
               <Tag_Id>90000371170</Tag_Id>
               <Config_Id>261448927065P</Config_Id>
               <Qty_Shipped>1</Qty_Shipped>
               <Catch_Weight>0</Catch_Weight>
               <Condition_Id>OK1</Condition_Id>
               <Ce_Under_Bond>N</Ce_Under_Bond>
               <Pallet_Id>CFPP0287495</Pallet_Id>
               <Container_Id>257084662521524253</Container_Id>
               <Carrier_Container_Id>5355642</Carrier_Container_Id>
            </DCLDET>
         </DCL>
         <DCL>
            <Line_Id>30</Line_Id>
            <Sku_Id>261448927070</Sku_Id>
            <Qty_Ordered>2</Qty_Ordered>
            <Qty_Shipped>2</Qty_Shipped>
            <Tracking_Level>BOX</Tracking_Level>
            <Config_Id>261448927070P</Config_Id>
            <Condition_Id>OK1</Condition_Id>
            <Host_Order_Id>001531609</Host_Order_Id>
            <Host_Line_Id>00137</Host_Line_Id>
            <Purchase_Order>SFS60627202450</Purchase_Order>
            <Product_Price_Currency>EUR</Product_Price_Currency>
            <Owner_Id>DECLARKSFP</Owner_Id>
            <Unallocatable>N</Unallocatable>
            <DCLUDF />
            <DCLDET>
               <Tag_Id>90000371172</Tag_Id>
               <Config_Id>261448927070P</Config_Id>
               <Qty_Shipped>1</Qty_Shipped>
               <Catch_Weight>0</Catch_Weight>
               <Condition_Id>OK1</Condition_Id>
               <Ce_Under_Bond>N</Ce_Under_Bond>
               <Pallet_Id>CFPP0287497</Pallet_Id>
               <Container_Id>257084662232653247</Container_Id>
               <Carrier_Container_Id>5355637</Carrier_Container_Id>
            </DCLDET>
            <DCLDET>
               <Tag_Id>90000371172</Tag_Id>
               <Config_Id>261448927070P</Config_Id>
               <Qty_Shipped>1</Qty_Shipped>
               <Catch_Weight>0</Catch_Weight>
               <Condition_Id>OK1</Condition_Id>
               <Ce_Under_Bond>N</Ce_Under_Bond>
               <Pallet_Id>CFPP0287497</Pallet_Id>
               <Container_Id>257084662232562574</Container_Id>
               <Carrier_Container_Id>5355636</Carrier_Container_Id>
            </DCLDET>
         </DCL>
         <DCL>
            <Line_Id>40</Line_Id>
            <Sku_Id>261448927075</Sku_Id>
            <Qty_Ordered>2</Qty_Ordered>
            <Qty_Shipped>2</Qty_Shipped>
            <Tracking_Level>BOX</Tracking_Level>
            <Config_Id>261448927075P</Config_Id>
            <Condition_Id>OK1</Condition_Id>
            <Host_Order_Id>001531609</Host_Order_Id>
            <Host_Line_Id>00138</Host_Line_Id>
            <Purchase_Order>SFS60627202450</Purchase_Order>
            <Product_Price_Currency>EUR</Product_Price_Currency>
            <Owner_Id>DECLARKSFP</Owner_Id>
            <Unallocatable>N</Unallocatable>
            <DCLUDF />
            <DCLDET>
               <Tag_Id>90000371174</Tag_Id>
               <Config_Id>261448927075P</Config_Id>
               <Qty_Shipped>1</Qty_Shipped>
               <Catch_Weight>0</Catch_Weight>
               <Condition_Id>OK1</Condition_Id>
               <Ce_Under_Bond>N</Ce_Under_Bond>
               <Pallet_Id>CFPP0287496</Pallet_Id>
               <Container_Id>257084662521524526</Container_Id>
               <Carrier_Container_Id>5355641</Carrier_Container_Id>
            </DCLDET>
            <DCLDET>
               <Tag_Id>90000371174</Tag_Id>
               <Config_Id>261448927075P</Config_Id>
               <Qty_Shipped>1</Qty_Shipped>
               <Catch_Weight>0</Catch_Weight>
               <Condition_Id>OK1</Condition_Id>
               <Ce_Under_Bond>N</Ce_Under_Bond>
               <Pallet_Id>CFPP0287495</Pallet_Id>
               <Container_Id>257084662521524253</Container_Id>
               <Carrier_Container_Id>5355642</Carrier_Container_Id>
            </DCLDET>
         </DCL>
         <DCL>
            <Line_Id>50</Line_Id>
            <Sku_Id>261448927080</Sku_Id>
            <Qty_Ordered>4</Qty_Ordered>
            <Qty_Shipped>4</Qty_Shipped>
            <Tracking_Level>BOX</Tracking_Level>
            <Config_Id>261448927080P</Config_Id>
            <Condition_Id>OK1</Condition_Id>
            <Host_Order_Id>001531609</Host_Order_Id>
            <Host_Line_Id>00139</Host_Line_Id>
            <Purchase_Order>SFS60627202450</Purchase_Order>
            <Product_Price_Currency>EUR</Product_Price_Currency>
            <Owner_Id>DECLARKSFP</Owner_Id>
            <Unallocatable>N</Unallocatable>
            <DCLUDF />
            <DCLDET>
               <Tag_Id>90000371176</Tag_Id>
               <Config_Id>261448927080P</Config_Id>
               <Qty_Shipped>3</Qty_Shipped>
               <Catch_Weight>0</Catch_Weight>
               <Condition_Id>OK1</Condition_Id>
               <Ce_Under_Bond>N</Ce_Under_Bond>
               <Pallet_Id>CFPP0287498</Pallet_Id>
               <Container_Id>257084662353264845</Container_Id>
               <Carrier_Container_Id>5355639</Carrier_Container_Id>
            </DCLDET>
            <DCLDET>
               <Tag_Id>90000371176</Tag_Id>
               <Config_Id>261448927080P</Config_Id>
               <Qty_Shipped>1</Qty_Shipped>
               <Catch_Weight>0</Catch_Weight>
               <Condition_Id>OK1</Condition_Id>
               <Ce_Under_Bond>N</Ce_Under_Bond>
               <Pallet_Id>CFPP0287498</Pallet_Id>
               <Container_Id>257084662353264858</Container_Id>
               <Carrier_Container_Id>5355640</Carrier_Container_Id>
            </DCLDET>
         </DCL>
      </DCH>
   </HEAD>
</DSV_DESADV0103>

我已经试过了,但是我迷路了。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes" />

    <xsl:key name="containerId" match="DSV_DESADV0103/HEAD/DCL/DCLDET" use="@Container_Id" />

    <xsl:template match="DSV_DESADV0103/HEAD/DCL/DCLDET">
        <AllUsers>
            <xsl:apply-templates select="DCL/DCLDET[generate-id(.)=generate-id(key('containerId',@Container_Id)[1])]"/>
        </AllUsers>
    </xsl:template>

    <xsl:template match="DSV_DESADV0103/HEAD/DCL/DCLDET">
        <Division value="{@Container_Id}">
            <xsl:for-each select="key('containerId', @Container_Id)">
                <User>
                    <id><xsl:value-of select="@Tag_Id" /></id>
                    <name><xsl:value-of select="@Qty_Shipped" /></name>
                </User>
            </xsl:for-each>
        </Division>
    </xsl:template>

</xsl:stylesheet>

你离得不远了,但是犯了一些小错误....

  1. 您的 XPath 表达式遗漏了 DCHDCLDCH 的子代,而不是 HEAD
  2. 的子代
  3. 您有两个匹配相同表达式 DSV_DESADV0103/HEAD/DCL/DCLDET 的模板(由于第 1 点,它们不会匹配任何内容)。我认为第一个模板应该只匹配 DSV_DESADV0103/HEAD any 因为否则里面的 xsl:apply-templates select anything
  4. 您在表达式中使用了 @container_id,但 container_id 不是属性,而是子节点

试试这个 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes" />

    <xsl:key name="containerId" match="DSV_DESADV0103/HEAD/DCH/DCL/DCLDET" use="Container_Id" />

    <xsl:template match="DSV_DESADV0103/HEAD">
        <AllUsers>
            <xsl:apply-templates select="DCH/DCL/DCLDET[generate-id(.)=generate-id(key('containerId',Container_Id)[1])]"/>
        </AllUsers>
    </xsl:template>

    <xsl:template match="DSV_DESADV0103/HEAD/DCH/DCL/DCLDET">
        <Division value="{Container_Id}">
            <xsl:for-each select="key('containerId', Container_Id)">
                <User>
                    <id><xsl:value-of select="Tag_Id" /></id>
                    <name><xsl:value-of select="Qty_Shipped" /></name>
                </User>
            </xsl:for-each>
        </Division>
    </xsl:template>
</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/6r5Gh4b

查看实际效果

请注意,您现有输出的原因是因为您之前的模板不匹配任何内容,而是使用了 XSLT 的 built-in templates,并且这些只是在它们找到它们的地方输出文本节点。

另请注意,您可以将第二个模板上的匹配简化为 <xsl:template match="DCLDET">,因为您实际上不需要匹配模式的完整路径。