xml - 如何计算 XSLT 1.0 中每个单元分组的节点总和?
问题描述
我想用 XSLT1.0 根据 XML 中的节点计算 XML 值的总和。我需要@Qty
Routes 表中所有可用值的总和。每个OrderTrans
from Routes 都有一个itedID
, 将匹配itemID
from Products。Products 表也有ItemDetails
节点,每个产品都有一个unitID
. 我的最终结果应该显示所有@Qty
分组的总和@unitID
。
以下是 XML 源代码:
<root>
<Routes>
<OrderTrans itemID="1">
<View Qty="5.00"/>
</OrderTrans>
<OrderTrans itemID="2">
<View Qty="200.00"/>
</OrderTrans>
<OrderTrans itemID="2">
<View Qty="10.00"/>
</OrderTrans>
<OrderTrans itemID="1">
<View Qty="20.00"/>
</OrderTrans>
</Routes>
<Products>
<Product itemID="2">
<ItemDetails unitID="KG"/>
</Product>
<Product itemID="1">
<ItemDetails unitID="Pcs"/>
</Product>
</Products>
</root>
这是我尝试过的一个片段:
<xsl:key name="groupkey" match="/root/Products/Product/ItemDetails" use="@unitID"/>
<xsl:template>
<xsl:for-each select="/root/Products/Product/ItemDetails[generate-id(.) = generate-id(key('groupkey',@unitID)[1])]">
<fo:block>Sum here:
<xsl:value select="sum(../../../Routes/OrderTrans/View/@Qty[@unitID = current()/@unitID])">
</fo:block>
</xsl:for-each>
</xslt:template>
结果应该是:
Sum here: 25.00 Pcs. 210.00 KG
解决方案
当您已经为每个产品拥有一个唯一节点时,我不明白为什么需要对任何东西进行分组。你可以简单地做:
<xsl:key name="order" match="OrderTrans" use="@itemID"/>
<xsl:template match="/root">
<your-root-here>
<xsl:for-each select="Products/Product">
<fo:block>
<xsl:text>Sum here: </xsl:text>
<xsl:value-of select="sum(key('order', @itemID)/View/@Qty)"/>
<xsl:text> </xsl:text>
<xsl:value-of select="ItemDetails/@unitID"/>
</fo:block>
</xsl:for-each>
</your-root-here>
</xsl:template>
推荐阅读
- django-rest-framework - DRF 嵌套序列化程序 - 对象没有属性
- windows - 登录到适用于 Linux 的 Windows 子系统时如何自动运行命令
- javascript - 在javascript中获取标签的数据计数属性值
- matlab - 如何在 tiledlayout 图上添加跨越 ylabel?
- mysql - 如何在我的表中添加一个从 001 自动递增到 999 的字段,除了它的原始 ID
- javascript - 运行除了 homebrew-brew 之外的 Mongodb 替代品
- android - How to getText from another Layout from editText Android
- pandas - 如何从数据集中绘制多条线作为国家的线图
- r - 基于 REGEX 更改列
- mysql - MySQL 查询以计算一年中的某天发生的次数