星期四, 二月 19, 2004

XSLT 求和模式

number.xml
<numbers>12 24 24 15 16 17</numbers>

total.xsl

<?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">
<xsl:template match="/">
<xsl:value-of select="sum(node()//workage[text()>2])"/>
------
<xsl:value-of select="sum(node()//workage(1|0002|4))"/>
------CR
<?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">
<xsl:template match="/">
<xsl:call-template name="total-number">
<xsl:with-param name="list" select="."/>
</xsl:template>
</xsl:template>
</xsl:template>
<xsl:template name="total-number">
<xsl:param name="list"/>
<xsl:variable name="wlist" select="concat(normalize-space($list),' ')"/>
<xsl:choose>
<xsl:when test="$wlist !=' '">
<xsl:variable name="first" select="substring-befor($wlist,' ')"/>
<xsl:variable name="rest" select="substring-after($wlist,' ')"/>
<xsl:variable name="total">
<xsl:call-template name="total-numbers">
<xsl:with-param name="list" select="$rest"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="number($first)+number($total)"/>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>