Muenchian 分组样式表返回重复的未分组节点
Muenchian grouping stylesheet returning duplicate ungrouped nodes
我正在尝试使用 Apache Xalan 按产品 ID 对产品变体列表进行分组。这是一个输入示例:
***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>
如您所见,虽然变体已正确分组,但在各自组中除第一个变体外的所有变体都重复了两次,一次在分组内,一次在分组外。
这是为什么?我该如何解决?
您需要阻止处理组中的第二个、第三个、第四个、.. 变体,否则默认身份转换会复制它们:
<xsl:template match="/root/variant[productId][not(generate-id() = generate-id(key('variants-by-productId', productId)[1]))]"/>
我正在尝试使用 Apache Xalan 按产品 ID 对产品变体列表进行分组。这是一个输入示例:
***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>
如您所见,虽然变体已正确分组,但在各自组中除第一个变体外的所有变体都重复了两次,一次在分组内,一次在分组外。
这是为什么?我该如何解决?
您需要阻止处理组中的第二个、第三个、第四个、.. 变体,否则默认身份转换会复制它们:
<xsl:template match="/root/variant[productId][not(generate-id() = generate-id(key('variants-by-productId', productId)[1]))]"/>