简体   繁体   English

我无法理解 XSLT 1.0 Muenchian 分组

[英]I cannot get my head around XSLT 1.0 Muenchian grouping

I'm basically just trying to group the DCLDET segments by the container ID, so i can create an additional tag that would be我基本上只是想通过容器 ID 对 DCLDET 段进行分组,所以我可以创建一个额外的标签

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

I've read up on the Muenchian grouping but nothing I do seems to work.我已经阅读了 Muenchian 分组,但我所做的一切似乎都不起作用。 I thought if I was grouping on the DCL/DCLDET segment by the Container Id, that's what I need?我想如果我按容器 ID 在 DCL/DCLDET 段上分组,这就是我需要的? But all i get back is outputted code with no structure (can post this if required but it's essentially just the text with no tags).但我得到的只是没有结构的输出代码(如果需要,可以发布这个,但它本质上只是没有标签的文本)。

The XML I have is this -我拥有的 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>

I have tried this, but i'm so lost.我试过这个,但我很迷茫。

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

You are not far off, but have made a number of small mistakes....你离得不远了,但犯了一些小错误......

  1. You have missed out DCH from your XPath expressions.您已经从 XPath 表达式中遗漏了DCH DCL is a child of DCH , not of HEAD DCLDCH的孩子,而不是HEAD
  2. You have two templates matching the same expression DSV_DESADV0103/HEAD/DCL/DCLDET (which won't match anything due to point 1 anyway).您有两个模板匹配相同的表达式DSV_DESADV0103/HEAD/DCL/DCLDET (由于第 1 点,它不会匹配任何内容)。 I think the first template should just match DSV_DESADV0103/HEAD any because otherwise the xsl:apply-templates inside it select anything anyway我认为第一个模板应该只匹配DSV_DESADV0103/HEAD any 因为否则里面的xsl:apply-templates选择任何东西
  3. You are using @container_id in your expressions, but container_id is not an attribute, but a child node您在表达式中使用@container_id ,但container_id不是属性,而是子节点

Try this XSLT instead试试这个 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>

See it in action at http://xsltfiddle.liberty-development.net/6r5Gh4bhttp://xsltfiddle.liberty-development.net/6r5Gh4b 上查看它的实际效果

Note, the reason for your existing output, is because your previous templates did not match anything, XSLT's built-in templates were used instead, and these just output text nodes where they find them.请注意,您现有输出的原因是因为您之前的模板没有匹配任何内容,而是使用了 XSLT 的内置模板,而这些只是在找到它们的地方输出文本节点。

Also note, you can simplify the match on the second template to just <xsl:template match="DCLDET"> as you don't actually need the full path for the pattern to match.另请注意,您可以将第二个模板上的匹配简化为<xsl:template match="DCLDET">因为您实际上不需要匹配模式的完整路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM