2012-10-18 63 views
3

剛開始學習xslt.just想知道如何計算國際玩家人數?另一件事是他的國際球員的平均身高?xslt統計相同內容的不同內容

<?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> 
    </footballclub> 

已嘗試以下;

數(//足球/播放/基礎[未(以下::基於= '國際')])XSLT:

<?xml version='1.0' encoding="utf-8"?> 
<xsl:stylesheet version='1.0' xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" version="4.0" /> 

<xsl:template match="/"> 
<html> 
<head > 
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<title>bombers FC</title> 
</head> 
<body> 
NUMBER OF INTERNATIONAL PLAYERS IS:<xsl:value-of select="count(//football/player/based[not(following::based='international')])"/> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

的結果應該是: 一些國際球員爲:2

回答

1

你的XPath需要改變一點點......

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

XSLT 1.0

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

    <xsl:template match="/"> 
     <html> 
      <head> 
       <title>bombers FC</title> 
      </head> 
      <body> 
       <p> 
        <xsl:text>NUMBER OF INTERNATIONAL PLAYERS IS: </xsl:text> 
        <xsl:value-of select="count(footballclub/player[based='international'])"/> 
       </p> 
       <p> 
        <xsl:text>AVERAGE HEIGHT OF INTERNATIONAL PLAYERS IS: </xsl:text> 
        <xsl:value-of select="sum(footballclub/player[based='international']/height) div count(footballclub/player[based='international'])"/> 
       </p> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

輸出

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

     <title>bombers FC</title> 
    </head> 
    <body> 
     <p>NUMBER OF INTERNATIONAL PLAYERS IS: 2</p> 
     <p>AVERAGE HEIGHT OF INTERNATIONAL PLAYERS IS: 5.375</p> 
    </body> 
</html> 
+0

另外固定 「有意」 而不是 「INTERNATIONAL」 錯字。 –

+0

謝謝,很高興它完成了。 – troy

+0

請你幫我解決國際球員的平均身高問題嗎?請 – troy