Xml 使用XSL保持一个运行总数
我有以下XML:Xml 使用XSL保持一个运行总数,xml,xslt,Xml,Xslt,我有以下XML: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="disp.xsl"?> <root> <node type="a"> <value>4</value> </node> <node type="b"> <value>2</value> &
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="disp.xsl"?>
<root>
<node type="a">
<value>4</value>
</node>
<node type="b">
<value>2</value>
</node>
<node type="a">
<value>3</value>
</node>
<node type="b">
<value>1</value>
</node>
</root
以下是我的XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="eachtype" match="node" use="@type" />
<xsl:template match="/root">
<html>
<body>
<xsl:for-each select="node">
Value: <xsl:value-of select="value"/> (Cumulative value: <xsl:variable name="temp" select="sum(preceding-sibling::node/value)+value"/><xsl:value-of select="$temp"/>)<br />
</xsl:for-each>
<hr />
<xsl:for-each select="node">
<xsl:variable name="thisType" select="@type"/>
type: <xsl:value-of select="$thisType" /> Value: <xsl:value-of select="value"/> (Cumulative value: <xsl:variable name="temp2" select="sum(preceding-sibling::node/value)+value"/><xsl:value-of select="$temp2"/>)<br />
</xsl:for-each>
<hr />
<xsl:for-each select="node[generate-id(.)=generate-id(key('eachtype',@type)[1])]">
<xsl:variable name="thisType" select="@type"/>
type: <xsl:value-of select="$thisType" /> Total: <xsl:value-of select="sum(/root/node[@type=$thisType]/value)"/> (Cumulative value: <xsl:variable name="temp2" select="sum(preceding-sibling::value)+value"/><xsl:value-of select="$temp2"/>)<br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
在最后两行中,我找不到一种方法来获得累积总数的正确值。在我第一次尝试使用XSL时,是否有XSL老手可以帮助我?这一转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kType" match="@type" use="."/>
<xsl:template match="/">
<xsl:for-each select=
"*/*/@type
[generate-id()
=
generate-id(key('kType', .)[1])
]">
<xsl:value-of select=
"concat('type: ', .,
' total: ', sum(/*/*[@type = current()]/value),
' cumulative total: ',
/*/*[@type = current()][last()]/value
+
sum(/*/*[@type = current()][last()]/preceding-sibling::*/value),
'
'
)
"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<root>
<node type="a">
<value>4</value>
</node>
<node type="a">
<value>3</value>
</node>
<node type="b">
<value>2</value>
</node>
<node type="b">
<value>1</value>
</node>
</root>
type: a total: 7 cumulative total: 7
type: b total: 3 cumulative total: 10
请注意,此解决方案效率不高。制动后,我将提供一个更有效的(递归)制动。:) 好问题(+1)。请参阅我的答案,以获得一个简单的解决方案。谢谢Dimitre,当XML节点按类型排序时,这种方法非常有效。但是,当类型的顺序不同时,例如类型a、b、a、b,累积总数会出错。我已尝试对for-each进行排序,但这不起作用,因为for-each正在对键进行排序。@Dave:您必须提供一个完整的示例——当节点未按类型排序时,使用xml——以及在这种情况下的结果。对不起,Dimitre,我现在更新了原始问题中的xml。我真的很感谢你的帮助!我已经研究过如何更改XML的生成方式(这只是一个手工制作的示例),但无法确保XML是按节点类型顺序创建的。@Dave:您遇到不同的问题并没有什么错——每当您指定该问题时(提供相应的XML文档以及所需的结果),我都愿意向您展示解决方案。目前,我的解决方案是针对您指定的问题,而不是针对您遇到的任何其他问题。
<root>
<node type="a">
<value>4</value>
</node>
<node type="a">
<value>3</value>
</node>
<node type="b">
<value>2</value>
</node>
<node type="b">
<value>1</value>
</node>
</root>
type: a total: 7 cumulative total: 7
type: b total: 3 cumulative total: 10