我正在尝试计算标准偏差,但仍然出现错误(此值不是节点集:<xsl:value-of select="sum(math:power(sum(playerweeklysalary - $the_avg), 2))"/>
)。我正在使用xmlns:math="http://exslt.org/math"中的数学函数。我对这样做的其他可能性持开放态度。
下面是 XML 的外观:
<players_db>
<players>
<player>
<playerbasicinfos>
<playerweeklysalary>2</playerweeklysalary>
</playerbasicinfos>
</player>
<player>
<playerbasicinfos>
<playerweeklysalary>10</playerweeklysalary>
</playerbasicinfos>
</player>
<player>
<playerbasicinfos>
<playerweeklysalary>3</playerweeklysalary>
</playerbasicinfos>
</player>
<player>
<playerbasicinfos>
<playerweeklysalary>6</playerweeklysalary>
</playerbasicinfos>
</player>
</players>
</players_db>
这里截取了 XSLT 1.0 代码:
<xsl:variable name="count_players_1">
<xsl:value-of select="count(/players_db/players/player)-1"/>
</xsl:variable>
<xsl:variable name="the_avg">
<xsl:value-of select="sum(/players_db/players/player/playerbasicinfos/playerweeklysalary) div count(/players_db/players/player)"/>
</xsl:variable>
<xsl:variable name="power2_sum_diff">
<xsl:for-each select="/players_db/players/player/playerbasicinfos">
<xsl:value-of select="sum(math:power(sum(playerweeklysalary - $the_avg), 2))"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="sqrt_all">
<xsl:value-of select="math:sqrt($power2_sum_diff div $count_players_1)"/>
</xsl:variable>
<xsl:text>Deviation here: </xsl:text> <xsl:value-of select="$sqrt_all"/>
试试这个方式:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:math="http://exslt.org/math"
extension-element-prefixes="exsl math">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/players_db/players">
<xsl:variable name="count" select="count(player)"/>
<xsl:variable name="sum" select="sum(player/playerbasicinfos/playerweeklysalary)"/>
<xsl:variable name="squares">
<xsl:for-each select="player">
<sq>
<xsl:value-of select="math:power(playerbasicinfos/playerweeklysalary, 2)"/>
</sq>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="sum-squares" select="sum(exsl:node-set($squares)/sq)" />
<xsl:variable name="variance" select="$sum-squares div ($count - 1) - math:power($sum, 2) div ($count * ($count - 1))" />
<xsl:text>Deviation here: </xsl:text>
<xsl:value-of select="math:sqrt($variance)"/>
</xsl:template>
</xsl:stylesheet>
注: 在 XSLT 1.0 中,不能直接对算术运算的结果求和。
添加:
为完整起见,下面是不支持 EXSLT math:power()
和math:sqrt()
扩展函数的处理器的方法:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/players_db/players">
<xsl:variable name="count" select="count(player)"/>
<xsl:variable name="sum" select="sum(player/playerbasicinfos/playerweeklysalary)"/>
<xsl:variable name="squares">
<xsl:for-each select="player">
<sq>
<xsl:value-of select="playerbasicinfos/playerweeklysalary * playerbasicinfos/playerweeklysalary"/>
</sq>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="sum-squares" select="sum(exsl:node-set($squares)/sq)" />
<xsl:variable name="variance" select="$sum-squares div ($count - 1) - ($sum * $sum) div ($count * ($count - 1))" />
<xsl:text>Deviation here: </xsl:text>
<xsl:call-template name="sqrt">
<xsl:with-param name="number" select="$variance"/>
</xsl:call-template></xsl:template>
<xsl:template name="sqrt">
<xsl:param name="number" select="0" />
<xsl:param name="try" select="1" />
<xsl:param name="iterations" select="10" />
<xsl:choose>
<xsl:when test="$try * $try = $number or not($iterations)">
<xsl:value-of select="$try"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="sqrt">
<xsl:with-param name="number" select="$number"/>
<xsl:with-param name="try" select="$try - ($try * $try - $number) div (2 * $try)"/>
<xsl:with-param name="iterations" select="$iterations - 1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>