如何使用 XSLT 1.0 获取直接下一个节点的总和
How to get sum of immediate next nodes using XSLT 1.0
我有这个 XML 文件,其中有这些节点:
<Rows>
<Row type="Comment">
<Amount>0.00</Amount>
</Row>
<Row type="Spec">
<Amount>10.00</Amount>
</Row>
<Row type="Spec">
<Amount>10.00</Amount>
</Row>
<Row type="Spec">
<Amount>10.00</Amount>
</Row>
<Row type="Comment">
<Amount>0.00</Amount>
</Row>
<Row type="Spec">
<Amount>20.00</Amount>
</Row>
<Row type="Spec">
<Amount>10.00</Amount>
</Row>
<Row type="Spec">
<Amount>20.00</Amount>
</Row>
</Rows>
结果应该是:
评论:30
评论:50
这些规范行将始终位于评论行之后。我需要对 Comment 行之后的 Spec 行求和。
我尝试在 XSLT 1.0 中使用 Preceeding 和 Following 函数,但它不起作用:
<xsl:value-of select="sum(../Row[@type='Spec']/Amount][following-sibling::row[1][@type='comment']])"/>
有人可以帮忙吗?
我建议你这样试试:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="spec" match="Row[@type='Spec']" use="generate-id(preceding-sibling::Row[@type='Comment'][1])" />
<xsl:template match="Rows">
<xsl:for-each select="Row[@type='Comment']">
<xsl:text>COMMENT: </xsl:text>
<xsl:value-of select="sum(key('spec', generate-id())/Amount)"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我有这个 XML 文件,其中有这些节点:
<Rows>
<Row type="Comment">
<Amount>0.00</Amount>
</Row>
<Row type="Spec">
<Amount>10.00</Amount>
</Row>
<Row type="Spec">
<Amount>10.00</Amount>
</Row>
<Row type="Spec">
<Amount>10.00</Amount>
</Row>
<Row type="Comment">
<Amount>0.00</Amount>
</Row>
<Row type="Spec">
<Amount>20.00</Amount>
</Row>
<Row type="Spec">
<Amount>10.00</Amount>
</Row>
<Row type="Spec">
<Amount>20.00</Amount>
</Row>
</Rows>
结果应该是: 评论:30 评论:50
这些规范行将始终位于评论行之后。我需要对 Comment 行之后的 Spec 行求和。
我尝试在 XSLT 1.0 中使用 Preceeding 和 Following 函数,但它不起作用:
<xsl:value-of select="sum(../Row[@type='Spec']/Amount][following-sibling::row[1][@type='comment']])"/>
有人可以帮忙吗?
我建议你这样试试:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="spec" match="Row[@type='Spec']" use="generate-id(preceding-sibling::Row[@type='Comment'][1])" />
<xsl:template match="Rows">
<xsl:for-each select="Row[@type='Comment']">
<xsl:text>COMMENT: </xsl:text>
<xsl:value-of select="sum(key('spec', generate-id())/Amount)"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>