我无法理解 XSLT 1.0 Muenchian 分组
Posted
技术标签:
【中文标题】我无法理解 XSLT 1.0 Muenchian 分组【英文标题】:I cannot get my head around XSLT 1.0 Muenchian grouping 【发布时间】:2019-12-07 16:40:23 【问题描述】:我基本上只是试图按容器 ID 对 DCLDET 段进行分组,所以我可以创建一个额外的标记,即
<Carton>
<Container_Id>
<SKU>
<Qty_Shipped>
</Carton>
我已经阅读了 Muenchian 分组,但我所做的一切似乎都不起作用。我想如果我按容器 ID 对 DCL/DCLDET 段进行分组,这就是我需要的吗?但是我得到的只是没有结构的输出代码(如果需要可以发布,但它本质上只是没有标签的文本)。
我拥有的 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 表达式中遗漏了
DCH
。 DCL
是 DCH
的孩子,而不是 HEAD
的孩子
您有两个模板匹配相同的表达式DSV_DESADV0103/HEAD/DCL/DCLDET
(无论如何,由于第 1 点,它不会匹配任何内容)。我认为第一个模板应该与 DSV_DESADV0103/HEAD
any 匹配,否则其中的 xsl:apply-templates
无论如何都会选择任何内容
您在表达式中使用了@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">
,因为您实际上并不需要匹配模式的完整路径。
【讨论】:
很好的解释。 @Tim C - 如果我想计算不同纸箱的数量,我会将容器 ID 的数量放在哪里?我尝试在任何地方添加它,但我得到 1 个或 8 个,而不是 6 个不同的纸箱。 @Dorbs - 你可以这样做.....<AllUsers count="count(DCH/DCL/DCLDET[generate-id(.)=generate-id(key('containerId',Container_Id)[1])])">
虽然你可能想使用一个变量来避免两次编写相同的长 xpath 表达式。以上是关于我无法理解 XSLT 1.0 Muenchian 分组的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Muenchian 分组 XSLT 1.0 在每个目录中按标题分组
使用 XSLT 对 HTML 输出进行分组(muenchian 分组?)