8,906
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="root/item">
<xsl:choose>
<xsl:when test = "@b">
<xsl:value-of select="number(@c)-number(@b)+number(@a)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="number(@c)+number(@a)" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
number(//item[@name='Jack']/@c)-number(//item[@name='Jack']/@b)+number(//item[@name='Jack']/@a)
<root>
<item name="Jack" a="1" b="2" c="3"/>
<item name="Rich" a="2" b="2" c="3"/>
<item name="Jack" a="3" b="2" c="3"/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:ckbk="ckbk" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:call-template name="ckbk:sum">
<xsl:with-param name="nodes" select="//item[@name='Jack']"/>
<xsl:with-param name="result" select="0"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="ckbk:sum">
<!-- Initialize nodes to empty node set -->
<xsl:param name="nodes" select="/.."/>
<xsl:param name="result" select="0"/>
<xsl:choose>
<xsl:when test="not($nodes)">
<xsl:value-of select="$result"/>
</xsl:when>
<xsl:otherwise>
<!-- call or apply template that will determine value of node
unless the node is literally the value to be summed -->
<xsl:variable name="value" select="$nodes[1]/@c - $nodes[1]/@b + $nodes[1]/@a"/>
<!-- recurse to sum rest -->
<xsl:call-template name="ckbk:sum">
<xsl:with-param name="nodes" select="$nodes[position() != 1]"/>
<xsl:with-param name="result" select="$result + $value"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>