你好,
XSLT新手,已经调试了一段时间
当前变量始终返回0
我需要通过每个(行)找到具有相同(D)值的所有(X)的总和
V和W是相关的,不知道如何连接"它们
示例:
Row (AAA123)[SomeDesc1] = 1.00 +
Row (BBB456)[SomeDesc1] = 3.00
SumOfSomeDesc1 = 4.00
仅限XSLT 1.0
XML:
<Root>
<Row>
<ID>AAA123</ID>
<V>
<X>1.00</X>
</V>
<V>
<X>2.00</X>
</V>
<MultipleFieldsInBetween />
<W>
<D>SomeDesc1</D>
</W>
<W>
<D>SomeDesc2</D>
</W>
</Row>
<Row>
<ID>BBB456</ID>
<V>
<X>3.00</X>
</V>
<V>
<X>4.00</X>
</V>
<MultipleFieldsInBetween />
<W>
<D>SomeDesc1</D>
</W>
<W>
<D>SomeDesc2</D>
</W>
</Row>
</Root>
XSLT Sum(当前):
<xsl:variable name="SumOfX" select="sum(//Row[ID/text()=$ID]/V[D/text()
=$Description])" />
答案 0 :(得分:2)
我会将其作为一个分组问题解决,首先确定唯一的描述,然后通过描述找到Row
,最后总结相同位置的元素:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:key name="desc-group" match="Row/W/D" use="."/>
<xsl:variable name="desc-groups" select="//Row/W/D[generate-id() = generate-id(key('desc-group', .)[1])]"/>
<xsl:key name="row-group" match="Row" use="W/D"/>
<xsl:template match="/Root">
<html>
<body>
<table>
<thead>
<tr>
<th>Description</th>
<th>Sum</th>
</tr>
</thead>
<tbody>
<xsl:apply-templates select="$desc-groups"/>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Row/W/D">
<tr>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:variable name="pos" select="count(.. | ../preceding-sibling::W)"/>
<xsl:value-of select="sum(key('row-group', .)/V[position() = $pos]/X)"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
结果是
<html>
<body>
<table>
<thead>
<tr>
<th>Description</th>
<th>Sum</th>
</tr>
</thead>
<tbody>
<tr>
<td>SomeDesc1</td>
<td>4</td>
</tr>
<tr>
<td>SomeDesc2</td>
<td>6</td>
</tr>
</tbody>
</table>
</body>
</html>