我是XSLT的新手。我需要合并和添加。
XML:
<OrderDetails>
<OrderDetail action="add">
<OrderedUnits>18</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295755</PONumber>
</OrderDetail>
<OrderDetail action="add">
<OrderedUnits>12</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295755</PONumber>
</OrderDetail>
<IOrderDetail action="add">
<OrderedUnits>18</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295762</PONumber>
</OrderDetail>
<OrderDetails>
如果LocationCode
,Date
和PONumber
字段匹配,我需要添加OrderedUnits
并仅将其设为一个条目。
预期输出XML:
<OrderDetails>
<OrderDetail action="add">
<OrderedUnits>30</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295755</PONumber>
</OrderDetail>
<IOrderDetail action="add">
<OrderedUnits>18</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295762</PONumber>
</OrderDetail>
<OrderDetails>
如何编写此XSLT?
答案 0 :(得分:0)
怎么样?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="utf-8" indent="no" />
<xsl:template match="/">
<OrderDetails>
<xsl:for-each select="//OrderDetail">
<xsl:variable name="this_date" select="Date" />
<xsl:variable name="this_pon" select="PONumber" />
<xsl:variable name="this_loc" select="LocationCode" />
<xsl:variable name="all" select="count(//OrderDetail[Date = $this_date and PONumber = $this_pon and LocationCode = $this_loc]) " />
<xsl:variable name="before" select="count(preceding-sibling::OrderDetail[Date = $this_date and PONumber = $this_pon and LocationCode = $this_loc]) " />
<xsl:if test="$before + 1 = $all">
<!-- we are in the last distinct order, display it -->
<OrderDetail action="add">
<OrderedUnits>
<xsl:value-of select="sum(//OrderDetail[Date = $this_date and PONumber = $this_pon and LocationCode = $this_loc]/OrderedUnits)" />
</OrderedUnits>
<Date>
<xsl:value-of select="Date" />
</Date>
<LocationCode>
<xsl:value-of select="LocationCode" />
</LocationCode>
<PONumber>
<xsl:value-of select="PONumber" />
</PONumber>
</OrderDetail>
</xsl:if>
</xsl:for-each>
</OrderDetails>
</xsl:template>
</xsl:stylesheet>
更正了XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<OrderDetails>
<OrderDetail action="add">
<OrderedUnits>18</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295755</PONumber>
</OrderDetail>
<OrderDetail action="add">
<OrderedUnits>12</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295755</PONumber>
</OrderDetail>
<OrderDetail action="add">
<OrderedUnits>18</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295762</PONumber>
</OrderDetail>
</OrderDetails>
结果(tested in):
<?xml version="1.0" encoding="UTF-8"?>
<OrderDetails>
<OrderDetail action="add">
<OrderedUnits>30</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295755</PONumber>
</OrderDetail>
<OrderDetail action="add">
<OrderedUnits>18</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295762</PONumber>
</OrderDetail>
</OrderDetails>
答案 1 :(得分:0)
由于您没有指定版本并且有更多人应该使用它,因此这是一个XSLT 2.0选项。
XML输入
<OrderDetails>
<OrderDetail action="add">
<OrderedUnits>18</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295755</PONumber>
</OrderDetail>
<OrderDetail action="add">
<OrderedUnits>12</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295755</PONumber>
</OrderDetail>
<OrderDetail action="add">
<OrderedUnits>18</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295762</PONumber>
</OrderDetail>
</OrderDetails>
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="OrderDetails">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each-group select="OrderDetail" group-by="string-join((Date,LocationCode,PONumber),'|')">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each-group select="current-group()/*" group-by="name()">
<xsl:apply-templates select="current-group()[1]"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="OrderedUnits">
<xsl:copy>
<xsl:value-of select="sum(current-group())"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML输出
<OrderDetails>
<OrderDetail action="add">
<OrderedUnits>30</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295755</PONumber>
</OrderDetail>
<OrderDetail action="add">
<OrderedUnits>18</OrderedUnits>
<Date>2013-09-30T00:00:00</Date>
<LocationCode>3202</LocationCode>
<PONumber>022548295762</PONumber>
</OrderDetail>
</OrderDetails>