xml - 如何对 XSLT 中的子节点求和
问题描述
我有传入 XSLT 的 xml 结构就像下面的代码。
<?xml version="1.0" encoding="UTF-8"?>
<TimeAccount>
<TimeAccount>
<userId>123</userId>
<timeAccountDetails>
<TimeAccountDetail>
<bookingType>MANUAL_ADJUSTMENT</bookingType>
<bookingUnit>HOURS</bookingUnit>
<bookingDate>2021-01-01T00:00:00.000</bookingDate>
<bookingAmount>190</bookingAmount>
</TimeAccountDetail>
<TimeAccountDetail>
<employeeTime>e885f88ccaa647abb00a8f84bcf5aa32</employeeTime>
<bookingType>EMPLOYEE_TIME</bookingType>
<bookingUnit>HOURS</bookingUnit>
<bookingDate>2021-04-19T00:00:00.000</bookingDate>
<bookingAmount>-8</bookingAmount>
</TimeAccountDetail>
<TimeAccountDetail>
<employeeTime>e885f88ccaa647abb00a8f84bcf5aa32</employeeTime>
<bookingType>MANUAL_ADJUSTMENT</bookingType>
<bookingUnit>HOURS</bookingUnit>
<bookingDate>2021-04-20T00:00:00.000</bookingDate>
<bookingAmount>120</bookingAmount>
</TimeAccountDetail>
<TimeAccountDetail>
<employeeTime>e885f88ccaa647abb00a8f84bcf5aa32</employeeTime>
<bookingType>EMPLOYEE_TIME</bookingType>
<bookingUnit>HOURS</bookingUnit>
<bookingDate>2021-04-21T00:00:00.000</bookingDate>
<bookingAmount>-8</bookingAmount>
</TimeAccountDetail>
</timeAccountDetails>
</TimeAccount>
<TimeAccount>
<userId>456</userId>
<timeAccountDetails>
<TimeAccountDetail>
<bookingType>MANUAL_ADJUSTMENT</bookingType>
<bookingUnit>HOURS</bookingUnit>
<bookingDate>2021-01-01T00:00:00.000</bookingDate>
<bookingAmount>190</bookingAmount>
</TimeAccountDetail>
<TimeAccountDetail>
<employeeTime>e885f88ccaa647abb00a8f84bcf5aa32</employeeTime>
<bookingType>EMPLOYEE_TIME</bookingType>
<bookingUnit>HOURS</bookingUnit>
<bookingDate>2021-04-19T00:00:00.000</bookingDate>
<bookingAmount>-8</bookingAmount>
</TimeAccountDetail>
<TimeAccountDetail>
<bookingType>MANUAL_ADJUSTMENT</bookingType>
<bookingUnit>HOURS</bookingUnit>
<bookingDate>2021-06-01T00:00:00.000</bookingDate>
<bookingAmount>100</bookingAmount>
</TimeAccountDetail>
<TimeAccountDetail>
<employeeTime>e885f88ccaa647abb00a8f84bcf5aa32</employeeTime>
<bookingType>EMPLOYEE_TIME</bookingType>
<bookingUnit>HOURS</bookingUnit>
<bookingDate>2021-04-21T00:00:00.000</bookingDate>
<bookingAmount>-8</bookingAmount>
</TimeAccountDetail>
</timeAccountDetails>
</TimeAccount>
</TimeAccount>
我希望通过为每个 userId 添加 bookingAmount 来输出如下
<TimeAccount>
<TimeAccount>
<ID>123</ID>
<bookingType>MANUAL_ADJUSTMENT</bookingType>
<bookingAmount>310</bookingAmount>
<TimeAccount>
<TimeAccount>
<ID>456</ID>
<bookingType>MANUAL_ADJUSTMENT</bookingType>
<bookingAmount>290</bookingAmount>
<TimeAccount>
</TimeAccount>
我编写了我的 xslt,如下所示,使用 for-each 和 if 条件,然后使用 SUM 函数。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<TimeAccount>
<xsl:apply-templates/>
</TimeAccount>
</xsl:template>
<xsl:template match="TimeAccount/TimeAccount">
<TimeAccount>
<ID><xsl:value-of select="userId"/></ID>
<bookingAmount>
<xsl:for-each select="timeAccountDetails/TimeAccountDetail">
<xsl:if test="bookingType != 'EMPLOYEE_TIME'">
<xsl:value-of select="sum(bookingAmount)"/>
</xsl:if>
</xsl:for-each>
</TimeAccount>
</xsl:template>
</xsl:stylesheet>
但输出不是添加 bookingAmounts 节点,而是并排打印节点。
<TimeAccount>
<TimeAccount>
<ID>123</ID>
<bookingAmount>190 120</bookingAmount>
<TimeAccount>
<TimeAccount>
<ID>456</ID>
<bookingAmount>190 100</bookingAmount>
<TimeAccount>
<TimeAccount>
我究竟做错了什么?我尝试在预订金额之前保留 /,例如 sum(/bookingAmount) 打印 null ,如果我输入 // 那么它会打印所有用户的 bookingAmounts 的总数。
解决方案
这个 xslt 给出了预期的结果:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="userId">
<ID>
<xsl:value-of select="text()"/>
</ID>
</xsl:template>
<xsl:template match="timeAccountDetails">
<bookingType>MANUAL_ADJUSTMENT</bookingType>
<bookingAmount>
<xsl:value-of select="sum(TimeAccountDetail[bookingType='MANUAL_ADJUSTMENT']/bookingAmount)"/>
</bookingAmount>
</xsl:template>
</xsl:stylesheet>
当您使用 xslt 3.0 时,您可以使用
<xsl:mode on-no-match="shallow-copy"/>
这与以下内容相同:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
并且只会复制您没有模板匹配的任何内容。
推荐阅读
- java - 如何从保存为 jpg 文件但不确切知道它是什么类型的图像文件中获取图像格式
- angular - Angular 应用程序无法使用跨域 API
- java - 无法找到或加载主类 VS 代码
- docker - SRE-Docker Compose 用于 CRON 作业
- r - 在 R 中的 facet_wrap ggplot 中的括号上方添加多个标题/文本
- reactjs - useEffect 被多次调用
- java - 如何正确连接 POST 请求中的 SpringBoot JPA 实体?
- flutter - 如何使用 Flutter 将字符串列表更新为 Firestore 中的文档字段?
- git - 有没有办法检查某个存储库是否是分叉?
- python - 重复 keras 模型的训练循环并收到警告