Я недавно опубликовал вопрос здесь для аналогичного требования, но теперь мне нужно сделать группу / сумму в XML с атрибутами. Я хочу суммировать все значения количественных атрибутов на основе атрибутов товара и статуса.
Входной XML:
<?xml version="1.0" encoding="UTF-8"?>
<StockAdjustments xmlns="http://vsystems.com/WS/StockAdjustments">
<StockAdjustment Article="36282" Client="AJA" Status="" Unit="ST" Quantity="-25" ReasonCode="OVERIG" Batch="" HostLocation="C" />
<StockAdjustment Article="36282" Client="AJA" Status="QUARANTAINE" Unit="ST" Quantity="25" ReasonCode="OVERIG" Batch="" HostLocation="C" />
<StockAdjustment Article="36282" Client="AJA" Status="" Unit="ST" Quantity="-25" ReasonCode="OVERIG" Batch="" HostLocation="C" />
<StockAdjustment Article="36282" Client="AJA" Status="QUARANTAINE" Unit="ST" Quantity="25" ReasonCode="OVERIG" Batch="" HostLocation="C" />
<StockAdjustment Article="36282" Client="AJA" Status="" Unit="ST" Quantity="-25" ReasonCode="OVERIG" Batch="" HostLocation="C" />
<StockAdjustment Article="36282" Client="AJA" Status="QUARANTAINE" Unit="ST" Quantity="25" ReasonCode="OVERIG" Batch="" HostLocation="C" />
</StockAdjustments>
Запрошенный вывод:
<?xml version="1.0" encoding="UTF-8"?>
<StockAdjustments xmlns="http://vsystems.com/WS/StockAdjustments">
<StockAdjustment Article="36282" Client="AJA" Status="" Unit="ST" Quantity="-75" ReasonCode="OVERIG" Batch="" HostLocation="C" />
<StockAdjustment Article="36282" Client="AJA" Status="QUARANTAINE" Unit="ST" Quantity="75" ReasonCode="OVERIG" Batch="" HostLocation="C" />
</StockAdjustments>
xsl (который еще не работает), который я создал:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:iws="http://vsystems.com/WS/StockAdjustments"
exclude-result-prefixes="iws">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="k1" match="/iws:StockAdjustment" use="concat(@Article, '|', @Status)"/>
<xsl:template match="/iws:StockAdjustments">
<xsl:copy>
22
<xsl:for-each select="/iws:StockAdjustment[generate-id()=generate-id(key('k1', concat(@Article,'|',@Status))[1])]">
<xsl:copy>
<xsl:copy-of select="@Article"/>
<QTY_EXPE>
<xsl:value-of select="sum(key('k1', concat(@Article,'|',@Status))/@Quantity)" />
</QTY_EXPE>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
xsl 1.0 (SAP PO)