XSL - 元素值的总和

  • 本文关键字:元素 XSL xml xslt
  • 更新时间 :
  • 英文 :


我有一个XML文件:

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <File>
        <Cars>
            <Car>
                <Color>Blue</Color>
                <Year>1988</Year>
                <Quantity>150</Quantity>
            </Car>
            <Car>
                <Color>Green</Color>
                <Year>1989</Year>
                <Quantity>200</Quantity>
            </Car>
        </Cars>
    </File>

还有一个XSL文件:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" encoding="utf-8" media-type="text/xml"/>
    <xsl:template match="File">
        <File>
            <vehicles>
                <xsl:for-each select="Cars/Car">
                    <vehicle>
                        <vehicleColor>
                            <xsl:value-of select="Color"/>
                        </vehicleColor>
                        <vehicleYear>
                            <xsl:value-of select="Year"/>
                        </vehicleYear>
                    </vehicle>
                </xsl:for-each>
            </vehicles>
            <TotalQuantity>
                    <xsl:value-of select="sum()"/> ///// Sum of quantity of all car (<Quantity>)
            </TotalQuantity>
        </File>
    </xsl:template>
</xsl:stylesheet>

我想制作每辆车的所有数量的总和,并在元素TotalQuantity中显示结果。在sum()方法中放什么?

我只使用XSL 1.0。我用 c# 类转换XslCompiledTransform XML

由于您当前的节点是 File ,您需要:

sum(Cars/Car/Quantity)

答案是肯定的。

最新更新