xslt 1.0的标准偏差

时间:2016-02-01 08:32:27

标签: xslt xslt-1.0 standard-deviation variance

大家早上好, 我,我试图计算标准差,但仍然得到错误(这个值不是一个节点集:<xsl:value-of select="sum(math:power(sum(playerweeklysalary - $the_avg), 2))"/>)。 我,我在xmlns中使用数学函数:math =&#34; 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"/>

1 个答案:

答案 0 :(得分:0)

以这种方式尝试:

<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>