Muenchian Grouping - 通过不出现在 xml 结果中进行分组
Posted
技术标签:
【中文标题】Muenchian Grouping - 通过不出现在 xml 结果中进行分组【英文标题】:Muenchian Grouping - Group by not appearing in xml result 【发布时间】:2021-11-12 18:30:19 【问题描述】:我有以下 CSV 文件,因此 XSD 看起来像这样:
123456|123456777|Green|Grün
123456|123456888|Red|Rot
987654|987654333|Yellow|Gelb
In XML format it looks like this:
<Product xmlns="http://F.FlatFileSchema1">
<Products xmlns="">
<product>123456</product>
<sku>123456777</sku>
<parentcolour_EN>Green</parentcolour_EN>
<parentcolour_DE>Grün</parentcolour_DE>
</Products>
<Products xmlns="">
<product>123456</product>
<sku>123456888</sku>
<parentcolour_EN>Red</parentcolour_EN>
<parentcolour_DE>Rot</parentcolour_DE>
</Products>
<Products xmlns="">
<product>987654</product>
<sku>987654333</sku>
<parentcolour_EN>Yellow</parentcolour_EN>
<parentcolour_DE>Gelb</parentcolour_DE>
</Products>
</Product>
目的是将其转换为具有以下格式的xml:
<?xml version="1.0" encoding="utf-8"?>
<enfinity xsi:schemaLocation="http://www.intershop.com/xml/ns/enfinity/7.0/xcs/impex catalog.xsd http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt dt.xsd" major="6" minor="1" family="enfinity" branch="enterprise" build="build" xmlns="http://www.intershop.com/xml/ns/enfinity/7.0/xcs/impex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dt="http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt">
<offer sku="123456777">
<custom-attributes>
<custom-attribute name="parentcolour" dt:dt="string" xml:lang="en-US">Green</custom-attribute>
<custom-attribute name="parentcolour" dt:dt="string" xml:lang="de-DE">Grün</custom-attribute>
</custom-attributes>
</offer>
<offer sku="123456888">
<custom-attributes>
<custom-attribute name="parentcolour" dt:dt="string" xml:lang="en-US">Red</custom-attribute>
<custom-attribute name="parentcolour" dt:dt="string" xml:lang="de-DE">Rot</custom-attribute>
</custom-attributes>
</offer>
<offer sku="123456">
<variations>
<mastered-products>
<mastered-product sku="123456777" domain="MasterRepository"/>
<mastered-product sku="123456888" domain="MasterRepository"/>
</mastered-products>
</variations>
</offer>
<offer sku="987654333">
<custom-attributes>
<custom-attribute name="parentcolour" dt:dt="string" xml:lang="en-US">Yellow</custom-attribute>
<custom-attribute name="parentcolour" dt:dt="string" xml:lang="de-DE">Gelb</custom-attribute>
</custom-attributes>
</offer>
<offer sku="987654">
<variations>
<mastered-products>
<mastered-product sku="987654333" domain="MasterRepository"/>
</mastered-products>
</variations>
</offer>
</enfinity>
正如您所见,所有 3 个 sku 都出现在 XML 中,但还需要在 sku 末尾出现一个附加产品,即 123456 出现在 123456777 和 123456888 之后,987654 出现在 987654333 之后。到目前为止我无法显示应用 Muenchian 分组后产品级别的变化块。我当前的 XSL 如下所示:
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s0="http://F.FlatFileSchema1" xmlns:xml="http://www.w3.org/XML/1998/namespace" exclude-result-prefixes="s0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="groupbyproduct" match="offer" use="product"/>
<xsl:template match="/s0:Product">
<enfinity xsi:schemaLocation="http://www.intershop.com/xml/ns/enfinity/7.0/xcs/impex catalog.xsd http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt dt.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.intershop.com/xml/ns/enfinity/7.0/xcs/impex" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:dt="http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt" major="6" minor="1" family="enfinity" branch="enterprise" build="build">
<xsl:for-each select="Products">
<offer sku="sku">
<custom-attributes>
<custom-attribute name="parentcolour" dt:dt="string" xml:lang="en-US">
<xsl:value-of select="parentcolour_EN/text()"/>
</custom-attribute>
<custom-attribute name="parentcolour" dt:dt="string" xml:lang="de-DE">
<xsl:value-of select="parentcolour_DE/text()"/>
</custom-attribute>
</custom-attributes>
</offer>
</xsl:for-each>
<!-- create a group for each unique product -->
<xsl:for-each select="offer[count(. | key('groupbyproduct', product)[1]) = 1]">
<offer sku="product">
<variations>
<mastered-products>
<!-- for each member of current group -->
<xsl:for-each select="key('groupbyproduct', product)">
<mastered-product sku="sku" domain="MasterRepository"/>
</xsl:for-each>
</mastered-products>
</variations>
</offer>
</xsl:for-each>
</enfinity>
</xsl:template>
</xsl:stylesheet>
【问题讨论】:
请编辑您的问题并显示 XML 输入。 很抱歉应该包括这个,只是把它放在问题的顶部,提前非常感谢! 【参考方案1】:密钥没有意义,我认为你想要<xsl:key name="groupbyproduct" match="Products" use="product"/>
,而不是你想要的第二个for-each
<!-- create a group for each unique product -->
<xsl:for-each select="Products[count(. | key('groupbyproduct', product)[1]) = 1]">
<offer sku="product">
<variations>
<mastered-products>
<!-- for each member of current group -->
<xsl:for-each select="key('groupbyproduct', product)">
<mastered-product sku="sku" domain="MasterRepository"/>
</xsl:for-each>
</mastered-products>
</variations>
</offer>
</xsl:for-each>
至于您的评论和您想要的完整结果,看起来好像首先分组然后在内部输出不同的数据应该给出您想要的顺序:
<xsl:for-each select="Products[count(. | key('groupbyproduct', product)[1]) = 1]">
<xsl:for-each select="key('groupbyproduct', product)">
<offer sku="sku">
<custom-attributes>
<custom-attribute name="parentcolour" dt:dt="string" xml:lang="en-US">
<xsl:value-of select="parentcolour_EN/text()"/>
</custom-attribute>
<custom-attribute name="parentcolour" dt:dt="string" xml:lang="de-DE">
<xsl:value-of select="parentcolour_DE/text()"/>
</custom-attribute>
</custom-attributes>
</offer>
</xsl:for-each>
<offer sku="product">
<variations>
<mastered-products>
<!-- for each member of current group -->
<xsl:for-each select="key('groupbyproduct', product)">
<mastered-product sku="sku" domain="MasterRepository"/>
</xsl:for-each>
</mastered-products>
</variations>
</offer>
</xsl:for-each>
【讨论】:
啊,我知道我很接近,这种工作但无论如何都可以订购数据,以便产品始终在 sku 级别之后出现?目前它显示所有 3 个 sku,然后显示 2 个产品,但我需要它显示 2 个 sku 及其对应的产品,然后显示最终的 sku 及其对应的产品 @tommyhmt,最初我只是试图将您现有的尝试转换为有意义的东西,您的键试图选择您输入中甚至没有的元素。至于顺序,请参阅编辑,仍然尝试重用您的代码,但嵌套方式不同。 这绝对是正确的,再次感谢您的帮助!以上是关于Muenchian Grouping - 通过不出现在 xml 结果中进行分组的主要内容,如果未能解决你的问题,请参考以下文章