Muenchian 分组样式表返回重复的未分组节点
Posted
技术标签:
【中文标题】Muenchian 分组样式表返回重复的未分组节点【英文标题】:Muenchian grouping stylesheet returning duplicate ungrouped nodes 【发布时间】:2020-10-24 11:25:39 【问题描述】:我正在尝试使用 Apache Xalan 按其 productId 对产品变体列表进行分组。这是一个输入样本:
***input_1.xml***
<?xml version="1.0" encoding="utf-8"?>
<root>
<variant>
<productId>1</productId>
<price>100</price>
<stock unit="item">10</stock>
<attributes>
<attribute name="color" value="red" />
</attributes>
</variant>
<variant>
<productId>1</productId>
<price>100</price>
<stock unit="item">8</stock>
<attributes>
<attribute name="color" value="blue" />
</attributes>
</variant>
<variant>
<productId>1</productId>
<price>150</price>
<stock unit="item">12</stock>
<attributes>
<attribute name="color" value="green" />
</attributes>
</variant>
<variant>
<productId>2</productId>
<price>200</price>
<stock unit="item">7</stock>
<attributes>
<attribute name="color" value="purple" />
<attribute name="material" value="faux-leather" />
</attributes>
</variant>
<variant>
<productId>3</productId>
<price>190</price>
<stock unit="item">22</stock>
<attributes>
<attribute name="color" value="yellow" />
<attribute name="size" value="XL" />
</attributes>
</variant>
<variant>
<productId>3</productId>
<price>180</price>
<stock unit="item">13</stock>
<attributes>
<attribute name="color" value="yellow" />
<attribute name="size" value="L" />
</attributes>
</variant>
<variant>
<productId>3</productId>
<price>170</price>
<stock unit="item">5</stock>
<attributes>
<attribute name="color" value="yellow" />
<attribute name="size" value="M" />
</attributes>
</variant>
<variant>
<productId>3</productId>
<price>170</price>
<stock unit="item">7</stock>
<attributes>
<attribute name="color" value="yellow" />
<attribute name="size" value="S" />
</attributes>
</variant>
<variant>
<productId>3</productId>
<price>180</price>
<stock unit="item">12</stock>
<attributes>
<attribute name="color" value="yellow" />
<attribute name="size" value="XS" />
</attributes>
</variant>
</root>
然后我在 shell 中使用以下命令:
xalan -in input_1.xml -xsl muenchian_1.xsl -out output_1.xml -indent 4
使用以下样式表转换输入:
***muenchian_1.xml***
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:strip-space elements="*" />
<xsl:output method="xml" indent="yes"/>
<xsl:key name="variants-by-productId" match="/root/variant" use="productId"/>
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/root/variant[productId][generate-id() =
generate-id(key('variants-by-productId', productId)[1])]" priority="1">
<product-listing-group>
<productId>
<xsl:value-of select="productId"/>
</productId>
<xsl:for-each select="key('variants-by-productId', productId)">
<xsl:call-template name="identity" />
</xsl:for-each>
</product-listing-group>
</xsl:template>
</xsl:stylesheet>
期望得到以下输出:
***expected_1.xml***
<?xml version="1.0" encoding="utf-8"?>
<root>
<product>
<productId>1</productId>
<variant>
<price>100</price>
<stock unit="item">10</stock>
<attributes>
<attribute name="color" value="red" />
</attributes>
</variant>
<variant>
<price>100</price>
<stock unit="item">8</stock>
<attributes>
<attribute name="color" value="blue" />
</attributes>
</variant>
<variant>
<price>150</price>
<stock unit="item">12</stock>
<attributes>
<attribute name="color" value="green" />
</attributes>
</variant>
</product>
<product>
<productId>2</productId>
<variant>
<price>200</price>
<stock unit="item">7</stock>
<attributes>
<attribute name="color" value="purple" />
<attribute name="material" value="faux-leather" />
</attributes>
</variant>
</product>
<product>
<productId>3</productId>
<variant>
<price>190</price>
<stock unit="item">22</stock>
<attributes>
<attribute name="color" value="yellow" />
<attribute name="size" value="XL" />
</attributes>
</variant>
<variant>
<price>180</price>
<stock unit="item">13</stock>
<attributes>
<attribute name="color" value="L" />
</attributes>
</variant>
<variant>
<price>170</price>
<stock unit="item">5</stock>
<attributes>
<attribute name="color" value="M" />
</attributes>
</variant>
<variant>
<price>170</price>
<stock unit="item">7</stock>
<attributes>
<attribute name="color" value="S" />
</attributes>
</variant>
<variant>
<price>180</price>
<stock unit="item">12</stock>
<attributes>
<attribute name="color" value="XS" />
</attributes>
</variant>
</product>
</root>
但我得到了:
***output_1.xml***
<?xml version="1.0" encoding="UTF-8"?>
<root>
<product-listing-group>
<productId>1</productId>
<variant>
<productId>1</productId>
<price>100</price>
<stock unit="item">10</stock>
<attributes>
<attribute name="color" value="red"/>
</attributes>
</variant>
<variant>
<productId>1</productId>
<price>100</price>
<stock unit="item">8</stock>
<attributes>
<attribute name="color" value="blue"/>
</attributes>
</variant>
<variant>
<productId>1</productId>
<price>150</price>
<stock unit="item">12</stock>
<attributes>
<attribute name="color" value="green"/>
</attributes>
</variant>
</product-listing-group>
<variant>
<productId>1</productId>
<price>100</price>
<stock unit="item">8</stock>
<attributes>
<attribute name="color" value="blue"/>
</attributes>
</variant>
<variant>
<productId>1</productId>
<price>150</price>
<stock unit="item">12</stock>
<attributes>
<attribute name="color" value="green"/>
</attributes>
</variant>
<product-listing-group>
<productId>2</productId>
<variant>
<productId>2</productId>
<price>200</price>
<stock unit="item">7</stock>
<attributes>
<attribute name="color" value="purple"/>
<attribute name="material" value="faux-leather"/>
</attributes>
</variant>
</product-listing-group>
<product-listing-group>
<productId>3</productId>
<variant>
<productId>3</productId>
<price>190</price>
<stock unit="item">22</stock>
<attributes>
<attribute name="color" value="yellow"/>
<attribute name="size" value="XL"/>
</attributes>
</variant>
<variant>
<productId>3</productId>
<price>180</price>
<stock unit="item">13</stock>
<attributes>
<attribute name="color" value="yellow"/>
<attribute name="size" value="L"/>
</attributes>
</variant>
<variant>
<productId>3</productId>
<price>170</price>
<stock unit="item">5</stock>
<attributes>
<attribute name="color" value="yellow"/>
<attribute name="size" value="M"/>
</attributes>
</variant>
<variant>
<productId>3</productId>
<price>170</price>
<stock unit="item">7</stock>
<attributes>
<attribute name="color" value="yellow"/>
<attribute name="size" value="S"/>
</attributes>
</variant>
<variant>
<productId>3</productId>
<price>180</price>
<stock unit="item">12</stock>
<attributes>
<attribute name="color" value="yellow"/>
<attribute name="size" value="XS"/>
</attributes>
</variant>
</product-listing-group>
<variant>
<productId>3</productId>
<price>180</price>
<stock unit="item">13</stock>
<attributes>
<attribute name="color" value="yellow"/>
<attribute name="size" value="L"/>
</attributes>
</variant>
<variant>
<productId>3</productId>
<price>170</price>
<stock unit="item">5</stock>
<attributes>
<attribute name="color" value="yellow"/>
<attribute name="size" value="M"/>
</attributes>
</variant>
<variant>
<productId>3</productId>
<price>170</price>
<stock unit="item">7</stock>
<attributes>
<attribute name="color" value="yellow"/>
<attribute name="size" value="S"/>
</attributes>
</variant>
<variant>
<productId>3</productId>
<price>180</price>
<stock unit="item">12</stock>
<attributes>
<attribute name="color" value="yellow"/>
<attribute name="size" value="XS"/>
</attributes>
</variant>
</root>
正如您所看到的,当变量被正确分组时,除了各自组中的第一个变量外,所有变量都重复了两次,一次在分组内,一次在分组外。
这是为什么?我该如何解决?
【问题讨论】:
【参考方案1】:您需要阻止处理组中的第二个、第三个、第四个、.. 变体,否则默认身份转换会复制它们:
<xsl:template match="/root/variant[productId][not(generate-id() = generate-id(key('variants-by-productId', productId)[1]))]"/>
【讨论】:
你能不能也告诉我为什么身份转换不复制第一个? 好吧,对于第一个,您已经使用xsl:template match="/root/variant[productId][generate-id() = generate-id(key('variants-by-productId', productId)[1])]"
设置了模板以输出包装器product-listing-group
,因此显然身份转换不会像match="@* | node()"
那样启动(其中这里node()
可以匹配)具有较低的优先级(默认情况下),因为任何match="variant"
或match="/root/variant..."
。但是谓词generate-id() = generate-id(key('variants-by-productId', productId)[1])
基本上是. is key('...', ...)[1]
的XSLT 1方式以上是关于Muenchian 分组样式表返回重复的未分组节点的主要内容,如果未能解决你的问题,请参考以下文章
如何正确使用 Muenchian 按元素对非常重复的标签进行分组?
XSLT1,Muenchian 分组,在 foreach 循环中列出分组节点