按工厂分组并对数量值求和 - XSLT
Group by Plant and Sum the quantity value - XSLT
我是 XSLT 的初学者,卡在了某个点上,几乎不需要帮助。我需要总结每个工厂的所有数量。
在下面找到 Input xml.
<?xml version='1.0' encoding='UTF-8'?>
<stock>
<records>
<Plant>1001</Plant>
<quantity>1381</quantity>
<StorageLocation>1001</StorageLocation>
</records>
<records>
<Plant>1001</Plant>
<quantity>20</quantity>
<StorageLocation>4001</StorageLocation>
</records>
<records>
<Plant>1002</Plant>
<quantity>0</quantity>
<StorageLocation>5001</StorageLocation>
</records>
<records>
<Plant>1002</Plant>
<quantity>28</quantity>
<StorageLocation>1901</StorageLocation>
</records>
<records>
<Plant>1003</Plant>
<quantity>1</quantity>
<StorageLocation>1006</StorageLocation>
</records>
<records>
<Plant>1003</Plant>
<quantity>0</quantity>
<StorageLocation>1001</StorageLocation>
</records>
</stock>
我在互联网上做了很多研究,找到了一段代码。它不工作,我无法修复它。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<!-- Match the stock element -->
<xsl:template match="records">
<WebshopResponseArea>
<!-- Group each records element by its plant -->
<xsl:for-each-group select="records" group-by="Plant">
<!-- Sum all the elements from the current group -->
<xsl:value-of select="sum(current-group()/quantity)" />
</xsl:for-each-group>
</WebshopResponseArea>
</xsl:template>
</xsl:stylesheet>
预期输出 xml 低于。
<?xml version='1.0' encoding='UTF-8'?>
<WebshopResponseArea>
<Records>
<AvailableStock>1401</AvailableStock>
<Plant>1001</Plant>
</Records>
<Records>
<AvailableStock>28</AvailableStock>
<Plant>1002</Plant>
</Records>
<Records>
<AvailableStock>1</AvailableStock>
<Plant>1003</Plant>
</Records>
</WebshopResponseArea>
非常感谢您的帮助。提前致谢。
只需更改您的匹配项
<xsl:template match="records">
至
<xsl:template match="stock">
还有一些上下文,它看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<!-- Match the stock element -->
<xsl:template match="stock">
<WebshopResponseArea>
<!-- Group each records element by its plant -->
<xsl:for-each-group select="records" group-by="Plant">
<Records>
<AvailableStock>
<xsl:value-of select="sum(current-group()/quantity)" />
</AvailableStock>
<xsl:copy-of select="current-group()[1]/Plant"/>
</Records>
</xsl:for-each-group>
</WebshopResponseArea>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<!-- Match the stock element -->
<xsl:template match="/*">
<WebshopResponseArea>
<!--Group each records element by its plant -->
<xsl:for-each-group select="//records" group-by="Plant">
<Records>
<Plant><xsl:value-of select="current-grouping-key()" /></Plant>
<Quantity><xsl:value-of select="sum(current-group()/quantity)"/></Quantity>
</Records>
</xsl:for-each-group>
</WebshopResponseArea>
</xsl:template>
</xsl:stylesheet>
以上代码符合我的要求。
我是 XSLT 的初学者,卡在了某个点上,几乎不需要帮助。我需要总结每个工厂的所有数量。 在下面找到 Input xml.
<?xml version='1.0' encoding='UTF-8'?>
<stock>
<records>
<Plant>1001</Plant>
<quantity>1381</quantity>
<StorageLocation>1001</StorageLocation>
</records>
<records>
<Plant>1001</Plant>
<quantity>20</quantity>
<StorageLocation>4001</StorageLocation>
</records>
<records>
<Plant>1002</Plant>
<quantity>0</quantity>
<StorageLocation>5001</StorageLocation>
</records>
<records>
<Plant>1002</Plant>
<quantity>28</quantity>
<StorageLocation>1901</StorageLocation>
</records>
<records>
<Plant>1003</Plant>
<quantity>1</quantity>
<StorageLocation>1006</StorageLocation>
</records>
<records>
<Plant>1003</Plant>
<quantity>0</quantity>
<StorageLocation>1001</StorageLocation>
</records>
</stock>
我在互联网上做了很多研究,找到了一段代码。它不工作,我无法修复它。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<!-- Match the stock element -->
<xsl:template match="records">
<WebshopResponseArea>
<!-- Group each records element by its plant -->
<xsl:for-each-group select="records" group-by="Plant">
<!-- Sum all the elements from the current group -->
<xsl:value-of select="sum(current-group()/quantity)" />
</xsl:for-each-group>
</WebshopResponseArea>
</xsl:template>
</xsl:stylesheet>
预期输出 xml 低于。
<?xml version='1.0' encoding='UTF-8'?>
<WebshopResponseArea>
<Records>
<AvailableStock>1401</AvailableStock>
<Plant>1001</Plant>
</Records>
<Records>
<AvailableStock>28</AvailableStock>
<Plant>1002</Plant>
</Records>
<Records>
<AvailableStock>1</AvailableStock>
<Plant>1003</Plant>
</Records>
</WebshopResponseArea>
非常感谢您的帮助。提前致谢。
只需更改您的匹配项
<xsl:template match="records">
至
<xsl:template match="stock">
还有一些上下文,它看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<!-- Match the stock element -->
<xsl:template match="stock">
<WebshopResponseArea>
<!-- Group each records element by its plant -->
<xsl:for-each-group select="records" group-by="Plant">
<Records>
<AvailableStock>
<xsl:value-of select="sum(current-group()/quantity)" />
</AvailableStock>
<xsl:copy-of select="current-group()[1]/Plant"/>
</Records>
</xsl:for-each-group>
</WebshopResponseArea>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<!-- Match the stock element -->
<xsl:template match="/*">
<WebshopResponseArea>
<!--Group each records element by its plant -->
<xsl:for-each-group select="//records" group-by="Plant">
<Records>
<Plant><xsl:value-of select="current-grouping-key()" /></Plant>
<Quantity><xsl:value-of select="sum(current-group()/quantity)"/></Quantity>
</Records>
</xsl:for-each-group>
</WebshopResponseArea>
</xsl:template>
</xsl:stylesheet>
以上代码符合我的要求。