我遇到了一个只能使用XSLT解决的问题。问题是我们不希望重复的XML标签具有相同的Id,但是在具有多个标签的方案中,两个标签的数量字段需要一起添加。这可以在下面的XML中轻松演示。
INPUT XML
<root>
<Line>
<Id>4</Id>
<sku>111111</sku>
<quantity>1</quantity>
</Line>
<Line>
<Id>4</Id>
<sku>111111</sku>
<quantity>2</quantity>
</Line>
<Line>
<Id>3</Id>
<sku>222222</sku>
<quantity>1</quantity>
</Line>
<Line>
<Id>3</Id>
<sku>222222</sku>
<quantity>1</quantity>
</Line>
</root>
渴望输出
<root>
<Line>
<Id>4</Id>
<sku>111111</sku>
<quantity>3</quantity>
</Line>
<Line>
<Id>3</Id>
<sku>222222</sku>
<quantity>2</quantity>
</Line>
</root>
XSLT
启动XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/root">
<root>
<xsl:apply-templates select="orderLine"/>
</root>
</xsl:template>
<xsl:template match="/root/oderLine">
<xsl:if test="not(sku = preceding-sibling::orderLine/sku)"> </xsl:if>
</xsl:template>
</xsl:stylesheet>
比较Id&amp; SKU
实施例
输入
<root>
<Line>
<Id>4</Id>
<sku>111111</sku>
<quantity>1</quantity>
</Line>
<Line>
<Id>4</Id>
<sku>111222</sku>
<quantity>2</quantity>
</Line>
<Line>
<Id>3</Id>
<sku>222222</sku>
<quantity>1</quantity>
</Line>
<Line>
<Id>3</Id>
<sku>222222</sku>
<quantity>1</quantity>a
</Line>
</root>
所需输出
<root>
<Line>
<Id>4</Id>
<sku>111111</sku>
<quantity>1</quantity>
</Line>
<Line>
<Id>4</Id>
<sku>111222</sku>
<quantity>2</quantity>
</Line>
<Line>
<Id>3</Id>
<sku>222222</sku>
<quantity>2</quantity>
</Line>
</root>
实际输出
<root>
<Line>
<Id>4</Id>
<sku>111111</sku>
<quantity>3</quantity>
</Line>
<Line>
<Id>3</Id>
<sku>222222</sku>
<quantity>2</quantity>
</Line>
</root>
正如您所看到的那样,它只匹配Id而不是SKU。
答案 0 :(得分:0)
使用XSLT 1.0使用Muechian grouping:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:key name="l-by-id" match="Line" use="Id"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Line[generate-id() = generate-id(key('l-by-id', Id)[1])]/quantity">
<xsl:copy>
<xsl:value-of select="sum(key('l-by-id', ../Id)/quantity)"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Line[generate-id() != generate-id(key('l-by-id', Id)[1])]"/>
</xsl:stylesheet>
[编辑] 以下是对Id和sku上的组的上述解决方案的改编:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:key name="l-by-id" match="Line" use="concat(Id, '|', sku)"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Line[generate-id() = generate-id(key('l-by-id', concat(Id, '|', sku))[1])]/quantity">
<xsl:copy>
<xsl:value-of select="sum(key('l-by-id', concat(../Id, '|', ../sku))/quantity)"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Line[generate-id() != generate-id(key('l-by-id', concat(Id, '|', sku))[1])]"/>
</xsl:stylesheet>