xslt 1.0 上的标准偏差
Standard Deviation on xslt 1.0
大家早上好,
我正在尝试计算标准偏差,但仍然出现错误(此值不是节点集:<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>
大家早上好,
我正在尝试计算标准偏差,但仍然出现错误(此值不是节点集:<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>