Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 如何使用xsl获取xml数据中的平均值_Xslt - Fatal编程技术网

Xslt 如何使用xsl获取xml数据中的平均值

Xslt 如何使用xsl获取xml数据中的平均值,xslt,Xslt,我试图得到以下内容的平均值 <SectorWeightings Currency="xxx"> <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="A B||16.324500000000" Date="8/31/2011" /> <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Val

我试图得到以下内容的平均值

 <SectorWeightings Currency="xxx">
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="A B||16.324500000000" Date="8/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="C||16.744400000000" Date="7/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="C S||10.177400000000" Date="8/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="C S||8.8950000000000" Date="7/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="E||12.841600000000" Date="8/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="E||13.551700000000" Date="7/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="Fi S||14.157900000000" Date="8/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="Fi S||15.221200000000" Date="7/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="H Care||12.508900000000" Date="7/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="H Care||13.198200000000" Date="8/31/2011" />
 </SectorWeightings>  

以前5行为例,计算数值和平均值

下面是改进的代码

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <xsl:template match="/">
    <html>
      <body>
        <h2> zzzzzzzzz  </h2>

        <table style="width:50%;font-size:12px;" cellspacing="0" cellpadding="0">
          <tr style="width:50%; text-align:left;background-color:E6F1F9;">
            <th>    xxxxx   </th>
            <th>    yyyyy     </th>
          </tr>

          <xsl:apply-templates
            select="FundPricePerf/Group[@Name='xxxxx']/SubGroup[@Name='S']
                    /Fund[@FundCode='vv']/SectorWeightings
                    /SectorWeightingsEntry[@Type='Sector Name||Sector Weight (%)']">
            <xsl:sort select="substring-after('||', @Value)" data-type="number"
                      order="descending" />
          </xsl:apply-templates>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="SectorWeightingsEntry">
   <xsl:if test="position() &lt;= 5">
    <tr>
      <td>
        <xsl:value-of select="format-number(substring-after(@Value, '||'), '#.#')"/>
      </td>
    </tr>
   </xsl:if>
  </xsl:template>

</xsl:stylesheet>

zzzzzzz
xxxxx
YYYY
有人能帮我计算平均数吗?我需要继续上面的代码。

您可以像这样使用
sum()
div
count()

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common"
  xmlns:so="http://stackoverflow.com/questions/14606819"
  exclude-result-prefixes="xsl exsl so">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="/">
  <average>
    <xsl:variable name="weights">
      <xsl:apply-templates select="*/SectorWeightingsEntry/@Value" mode="extract-weight" />
    </xsl:variable> 
    <xsl:variable name="w" select="exsl:node-set($weights)/so:weight" />
    <xsl:value-of select="sum( $w) div count( $w)" />
  </average>
</xsl:template>

<xsl:template match="@Value" mode="extract-weight">
  <so:weight>
    <xsl:value-of select="substring-after(.,'||')" />
  </so:weight>
</xsl:template>

</xsl:stylesheet>

这就产生了

<average>13.362079999999997</average>
13.36207999997
您可以像这样使用
sum()
div
count()

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common"
  xmlns:so="http://stackoverflow.com/questions/14606819"
  exclude-result-prefixes="xsl exsl so">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="/">
  <average>
    <xsl:variable name="weights">
      <xsl:apply-templates select="*/SectorWeightingsEntry/@Value" mode="extract-weight" />
    </xsl:variable> 
    <xsl:variable name="w" select="exsl:node-set($weights)/so:weight" />
    <xsl:value-of select="sum( $w) div count( $w)" />
  </average>
</xsl:template>

<xsl:template match="@Value" mode="extract-weight">
  <so:weight>
    <xsl:value-of select="substring-after(.,'||')" />
  </so:weight>
</xsl:template>

</xsl:stylesheet>

这就产生了

<average>13.362079999999997</average>
13.36207999997

请尝试一下:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <xsl:template match="/">
    <xsl:variable name="items"
            select="FundPricePerf/Group[@Name='xxxxx']/SubGroup[@Name='S']
                    /Fund[@FundCode='vv']/SectorWeightings
                    /SectorWeightingsEntry[@Type='Sector Name||Sector Weight (%)']" />
    <html>
      <body>
        <h2> zzzzzzzzz  </h2>

        <table style="width:50%;font-size:12px;" cellspacing="0" cellpadding="0">
          <tr style="width:50%; text-align:left;background-color:E6F1F9;">
            <th>    xxxxx   </th>
            <th>    yyyyy     </th>
          </tr>

          <xsl:apply-templates select="$items">
            <xsl:sort select="substring-after(@Value, '||')" data-type="number"
                      order="descending" />
          </xsl:apply-templates>
        </table>

        <xsl:call-template name="ShowAverage">
          <xsl:with-param name="items" select="$items" />
        </xsl:call-template>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="SectorWeightingsEntry">
    <xsl:if test="position() &lt;= 5">
      <tr>
        <td>
          <xsl:value-of select="format-number(substring-after(@Value, '||'), '#.#')"/>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>

  <xsl:template name="ShowAverage">
    <xsl:param name="items" />

    <xsl:variable name="sumTopItems">
      <xsl:choose>
        <xsl:when test="$items">
          <xsl:apply-templates select="$items" mode="sumItems">
            <xsl:with-param name="items" select="$items" />
            <xsl:sort select="substring-after(@Value, '||')" data-type="number"
                      order="descending" />
          </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="itemsToAvg"
                  select="5 * (count($items) >= 5 or not($items)) + 
                              count($items) * (count($items) &lt; 5)" />

    <xsl:value-of select="concat('Average: ', 
                  format-number($sumTopItems div $itemsToAvg, '#.#'))"/>
  </xsl:template>

  <xsl:template match="SectorWeightingsEntry" mode="sumItems">
    <xsl:param name="items" />
    <xsl:param name="limit" select="5" />
    <xsl:param name="tally" select="0" />

    <xsl:if test="position() = 1">
      <xsl:if test="$limit &lt;= 0">
        <xsl:value-of select="$tally"/>
      </xsl:if>
      <xsl:if test="$limit > 0">
        <xsl:variable name="remaining"
                      select="$items[generate-id(.) != generate-id(current())]" />
        <xsl:apply-templates select="$remaining" mode="sumItems">
          <xsl:with-param name="items" select="$remaining" />
          <xsl:with-param name="limit" select="$limit - 1" />
          <xsl:with-param name="tally"
                          select="$tally + substring-after(@Value, '||')" />
          <xsl:sort select="substring-after(@Value, '||')" data-type="number"
                    order="descending" />
        </xsl:apply-templates>
      </xsl:if>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

zzzzzzz
xxxxx
YYYY
0
在示例XML上运行时的输出:

<html>
  <body>
    <h2> zzzzzzzzz  </h2>
    <table style="width:50%;font-size:12px;" cellspacing="0" cellpadding="0">
      <tr style="width:50%; text-align:left;background-color:E6F1F9;">
        <th>    xxxxx   </th>
        <th>    yyyyy     </th>
      </tr>
      <tr>
        <td>16.7</td>
      </tr>
      <tr>
        <td>16.3</td>
      </tr>
      <tr>
        <td>15.2</td>
      </tr>
      <tr>
        <td>14.2</td>
      </tr>
      <tr>
        <td>13.6</td>
      </tr>
    </table>Average: 15.2</body>
</html>

zzzzzzz
xxxxx
YYYY
16.7
16.3
15.2
14.2
13.6
平均:15.2

请尝试一下:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <xsl:template match="/">
    <xsl:variable name="items"
            select="FundPricePerf/Group[@Name='xxxxx']/SubGroup[@Name='S']
                    /Fund[@FundCode='vv']/SectorWeightings
                    /SectorWeightingsEntry[@Type='Sector Name||Sector Weight (%)']" />
    <html>
      <body>
        <h2> zzzzzzzzz  </h2>

        <table style="width:50%;font-size:12px;" cellspacing="0" cellpadding="0">
          <tr style="width:50%; text-align:left;background-color:E6F1F9;">
            <th>    xxxxx   </th>
            <th>    yyyyy     </th>
          </tr>

          <xsl:apply-templates select="$items">
            <xsl:sort select="substring-after(@Value, '||')" data-type="number"
                      order="descending" />
          </xsl:apply-templates>
        </table>

        <xsl:call-template name="ShowAverage">
          <xsl:with-param name="items" select="$items" />
        </xsl:call-template>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="SectorWeightingsEntry">
    <xsl:if test="position() &lt;= 5">
      <tr>
        <td>
          <xsl:value-of select="format-number(substring-after(@Value, '||'), '#.#')"/>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>

  <xsl:template name="ShowAverage">
    <xsl:param name="items" />

    <xsl:variable name="sumTopItems">
      <xsl:choose>
        <xsl:when test="$items">
          <xsl:apply-templates select="$items" mode="sumItems">
            <xsl:with-param name="items" select="$items" />
            <xsl:sort select="substring-after(@Value, '||')" data-type="number"
                      order="descending" />
          </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="itemsToAvg"
                  select="5 * (count($items) >= 5 or not($items)) + 
                              count($items) * (count($items) &lt; 5)" />

    <xsl:value-of select="concat('Average: ', 
                  format-number($sumTopItems div $itemsToAvg, '#.#'))"/>
  </xsl:template>

  <xsl:template match="SectorWeightingsEntry" mode="sumItems">
    <xsl:param name="items" />
    <xsl:param name="limit" select="5" />
    <xsl:param name="tally" select="0" />

    <xsl:if test="position() = 1">
      <xsl:if test="$limit &lt;= 0">
        <xsl:value-of select="$tally"/>
      </xsl:if>
      <xsl:if test="$limit > 0">
        <xsl:variable name="remaining"
                      select="$items[generate-id(.) != generate-id(current())]" />
        <xsl:apply-templates select="$remaining" mode="sumItems">
          <xsl:with-param name="items" select="$remaining" />
          <xsl:with-param name="limit" select="$limit - 1" />
          <xsl:with-param name="tally"
                          select="$tally + substring-after(@Value, '||')" />
          <xsl:sort select="substring-after(@Value, '||')" data-type="number"
                    order="descending" />
        </xsl:apply-templates>
      </xsl:if>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

zzzzzzz
xxxxx
YYYY
0
在示例XML上运行时的输出:

<html>
  <body>
    <h2> zzzzzzzzz  </h2>
    <table style="width:50%;font-size:12px;" cellspacing="0" cellpadding="0">
      <tr style="width:50%; text-align:left;background-color:E6F1F9;">
        <th>    xxxxx   </th>
        <th>    yyyyy     </th>
      </tr>
      <tr>
        <td>16.7</td>
      </tr>
      <tr>
        <td>16.3</td>
      </tr>
      <tr>
        <td>15.2</td>
      </tr>
      <tr>
        <td>14.2</td>
      </tr>
      <tr>
        <td>13.6</td>
      </tr>
    </table>Average: 15.2</body>
</html>

zzzzzzz
xxxxx
YYYY
16.7
16.3
15.2
14.2
13.6
平均:15.2

可能重复:否,请检查xml。在我的问题中,数据在节点中显示为属性。然后选择属性而不是节点。没什么大不了的。请参阅上的本教程。可能重复:否,请检查xml。在我的问题中,数据在节点中显示为属性。然后选择属性而不是节点。没什么大不了的。请参阅本教程。什么是
xmlns:so=”http://stackoverflow.com/questions/14606819“
?我不明白为什么要这样做here@menukadevinda它是一个本地名称空间,用于中间结果。它的目的是将中间结果与正常输入分离,定向运动没有意外冲突。什么是
xmlns:so=”http://stackoverflow.com/questions/14606819“
?我不明白为什么要这样做here@menukadevinda它是一个本地名称空间,用于中间结果。它的目的是将中间结果与正常输入分离,定向运动没有意外冲突。