1

之前发布过关于计数的问题;我正在寻找一种方法来计算国际平均身高,以及显示从最高到最小高度的高低功能......

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

1 回答 1

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
于 2012-10-19T17:27:03.337 回答