如何在XSLT中使用for:each添加两个xml标记值?
我想在COMS级别为每个CDS/CD节点添加此输入XML中的值。我期望的输出如下如何在XSLT中使用for:each添加两个xml标记值?,xml,xslt,sum,Xml,Xslt,Sum,我想在COMS级别为每个CDS/CD节点添加此输入XML中的值。我期望的输出如下 <?xml version="1.0"?> <CDS> <CD> <COMS> <COMP> <VAL>1.220</VAL>
<?xml version="1.0"?>
<CDS>
<CD>
<COMS>
<COMP>
<VAL>1.220</VAL>
</COMP>
<COMP>
<VAL>2.220</VAL>
</COMP>
</COMS>
</CD>
</CDS>
<CDS>
<CD>
<COMS>
<COMP>
<VAL>5.2</VAL>
</COMP>
<COMP>
<VAL>6.5</VAL>
</COMP>
</COMS>
</CD>
</CDS>
<CDS>
<CD>
<COMS>
<COMP>
<VAL>4.2</VAL>
</COMP>
<COMP>
<VAL>5.8</VAL>
</COMP>
</COMS>
</CD>
</CDS>
1.220
2.220
5.2
6.5
4.2
5.8
输出XML:
<CDS>
<CD>
<COMS>
<COMP>
<SUM_VALUE>3.44</SUM_VALUE>
</COMP>
</COMS>
</CD>
<CD>
<COMS>
<COMP>
<SUM_VALUE>11.7</SUM_VALUE>
</COMP>
</COMS>
</CD>
<CD>
<COMS>
<COMP>
<SUM_VALUE>10</SUM_VALUE>
</COMP>
</COMS>
</CD>
</CDS>
3.44
11.7
10
我的XSLT代码只尝试对所有值求和,并给出最终输出。如果有人能解释如何将for:each循环中的xml值求和到相应的comp级别,那将很有帮助。假设根元素名为
root
。请尝试以下样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="COMS">
<xsl:copy>
<COMP>
<SUM_VALUE><xsl:value-of select="sum(descendant::VAL)"/></SUM_VALUE>
</COMP>
</xsl:copy>
</xsl:template>
<xsl:template match="CDS">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="root">
<CDS>
<xsl:apply-templates/>
</CDS>
</xsl:template>
</xsl:stylesheet>
给定格式良好的输入:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<CDS>
<CD>
<COMS>
<COMP>
<VAL>1.220</VAL>
</COMP>
<COMP>
<VAL>2.220</VAL>
</COMP>
</COMS>
</CD>
</CDS>
<CDS>
<CD>
<COMS>
<COMP>
<VAL>5.2</VAL>
</COMP>
<COMP>
<VAL>6.5</VAL>
</COMP>
</COMS>
</CD>
</CDS>
<CDS>
<CD>
<COMS>
<COMP>
<VAL>4.2</VAL>
</COMP>
<COMP>
<VAL>5.8</VAL>
</COMP>
</COMS>
</CD>
</CDS>
</root>
1.220
2.220
5.2
6.5
4.2
5.8
以下样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="COMS">
<xsl:copy>
<COMP>
<SUM_VALUE><xsl:value-of select="sum(descendant::VAL)"/></SUM_VALUE>
</COMP>
</xsl:copy>
</xsl:template>
<xsl:template match="CDS">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="root">
<CDS>
<xsl:apply-templates/>
</CDS>
</xsl:template>
</xsl:stylesheet>
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<CDS>
<xsl:for-each select="root/CDS/CD">
<CD>
<COMS>
<COMP>
<SUM_VALUE><xsl:value-of select="sum(COMS/COMP/VAL)" /></SUM_VALUE>
</COMP>
</COMS>
</CD>
</xsl:for-each>
</CDS>
</xsl:template>
</xsl:stylesheet>
将返回:
<?xml version="1.0" encoding="UTF-8"?>
<CDS>
<CD>
<COMS>
<COMP>
<SUM_VALUE>3.44</SUM_VALUE>
</COMP>
</COMS>
</CD>
<CD>
<COMS>
<COMP>
<SUM_VALUE>11.7</SUM_VALUE>
</COMP>
</COMS>
</CD>
<CD>
<COMS>
<COMP>
<SUM_VALUE>10</SUM_VALUE>
</COMP>
</COMS>
</CD>
</CDS>
3.44
11.7
10
注意:
基于Java的引擎(如Saxon或Xalan)可能返回
3.4400000000000004
作为1.22+2.22之和。无精度问题的解决方案-XSLT 2.0示例
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<CDS>
<xsl:for-each select="root/CDS/CD">
<CD>
<COMS>
<COMP>
<SUM_VALUE><xsl:value-of select="sum(COMS/COMP/VAL)" /></SUM_VALUE>
</COMP>
</COMS>
</CD>
</xsl:for-each>
</CDS>
</xsl:template>
</xsl:stylesheet>
如果@michael.hor257k提到的数字精度问题无法容忍,您可以选择使用format-number()
进行一些处理,但这些处理并不总是会产生令人满意的结果
如果XSLT2.0是一个选项,那么可以使用xs:decimal
数据类型
编辑
以下是根据评论员@michael.hor257k的要求,总结
元素的所有
子元素的完整解决方案,无论找到多少子元素
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<CDS>
<xsl:for-each select="root/CDS/CD">
<CD>
<COMS>
<COMP>
<SUM_VALUE><xsl:apply-templates select="*/COMP[1]" /></SUM_VALUE>
</COMP>
</COMS>
</CD>
</xsl:for-each>
</CDS>
</xsl:template>
<xsl:template match="COMP">
<xsl:param name="sum" select="0" />
<xsl:variable name="newsum">
<xsl:value-of select="xs:decimal(VAL) + xs:decimal($sum)"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="following-sibling::*[1]">
<xsl:apply-templates select="following-sibling::*[1]">
<xsl:with-param name="sum" select="$newsum" />
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$newsum"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
此类解决方案需要一个支持XSLT2.0的XSLT处理器,如Saxon 9.5
更多信息,特别是有关XSLT 1.0方法的信息,请参阅:请发布XSLT代码并修复输入,使其格式正确(当前没有根元素)。精度问题可以通过使用XSLT 2.0解决,或者-如果这不是一个选项-使用
format-number()
。但这可能很棘手。更多信息请参见@codefan“精度问题可以通过使用XSLT 2.0解决”怎么办?请参见我使用XSLT 2.0解决方案的答案“逐个访问值”在我看来,治疗比疾病更糟糕……我相信舍入/格式化到最接近的分(或mill,如果这是所需的精度)在这种情况下应该足够了我不会拿给定的例子来暗示求和数的数目总是2,而使用命名递归模板进行简单加法的替代方法是非常不吸引人的另外,谁是休?@michael.hor257k Hugh?我还写道:这可以通过递归模板来解决。但这不是OP的问题。OP没有回应你关于更多信息的请求。我们所拥有的只是一个破碎的XML,每个XML中有两个元素。。。。我的评论是为了给前来询问你关于精度问题的话的人们一个额外的提示。顺便说一句,我真的很感谢你简洁明了的解决方案。事实上,今天早上你得到的是我的支持票:-)@michael.hor257k尽管如此,我还是明白了你的意思,并将相应地更新我的答案。@michael.hor257k添加了完整的XSLT2.0示例。希望你更喜欢它。。。