平均和低到高的功能

时间:2012-10-19 16:34:02

标签: xml xslt xslt-1.0

之前已经发布了有关计数的问题;我正在寻找一种如何提出国际平均高度的方法,以及从最高到最小的高低功能......

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="soccer.xslt"?>
<footballclub>
    <player>
        <based>international</based>
        <height>5.5</height >
        <build>medium</build>
        <age>24</age>
    </player>
    <player>
        <based>local</based>
        <height>5.5</height >
        <build>medium</build>
        <age>24</age>
    </player>
    <player>
        <based>international</based>
        <height>5.5</height >
        <build>medium</build>
        <age>24</age>
    </player>
    <player>
        <based>local</based>
        <height>5.5</height >
        <build>medium</build>
        <age>24</age>
    </player>
    <player>
        <based>local</based>
        <height>7.5</height >
        <build>medium</build>
        <age>34</age>
    </player>
    <player>
        <based>international</based>
        <height>5.9</height >
        <build>large</build>
        <age>26</age>
    </player>
</footballclub>

1 个答案:

答案 0 :(得分:1)

此转化

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:variable name="vInter" select="/*/*[based='international']"/>

 <xsl:template match="/">
     Average height: <xsl:copy-of select="sum($vInter/height) div count($vInter)"/>

     Height high to low:<xsl:text/>

     <xsl:for-each select="$vInter/height">
       <xsl:sort data-type="number" order="descending"/>
       <xsl:value-of select="concat('&#xA;      ', .)"/>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档

<footballclub>
    <player>
        <based>international</based>
        <height>5.5</height >
        <build>medium</build>
        <age>24</age>
    </player>
    <player>
        <based>local</based>
        <height>5.5</height >
        <build>medium</build>
        <age>24</age>
    </player>
    <player>
        <based>international</based>
        <height>5.5</height >
        <build>medium</build>
        <age>24</age>
    </player>
    <player>
        <based>local</based>
        <height>5.5</height >
        <build>medium</build>
        <age>24</age>
    </player>
    <player>
        <based>local</based>
        <height>7.5</height >
        <build>medium</build>
        <age>34</age>
    </player>
    <player>
        <based>international</based>
        <height>5.9</height >
        <build>large</build>
        <age>26</age>
    </player>
</footballclub>

生成想要的正确结果

     Average height: 5.633333333333333

     Height high to low:
        5.9
        5.5
        5.5