根据属性值选择节点

时间:2013-08-07 19:03:32

标签: xml xslt-1.0 xslt-grouping muenchian-grouping

我一直在努力确定如何获取游戏结果的XML文件(团队和最终得分),并生成一个团队排名列表,显示每个团队以及他们根据游戏状态获胜,丢失或捆绑的次数(饰演)。我能够在游戏状态(已播放和待定)上显示团队排名。我想展示只参加比赛的球队排名。非常感谢任何帮助。

这是我的xml代码:

<Schedule>
    <game status="played">
      <Home_Team>A</Home_Team>
      <Away_Team>B</Away_Team>
      <Date>2013-06-15</Date>
      <Home_Team_Score>3</Home_Team_Score>
      <Away_Team_Score>3</Away_Team_Score>
   </game>

   <game status="played">
      <Home_Team>A</Home_Team>
      <Away_Team>C</Away_Team>
      <Date>2013-06-17</Date>
      <Home_Team_Score>7</Home_Team_Score>
      <Away_Team_Score>4</Away_Team_Score>
   </game>

   <game status="played">
      <Home_Team>C</Home_Team>
      <Away_Team>A</Away_Team>
      <Date>2013-06-19</Date>
      <Home_Team_Score>3</Home_Team_Score>
      <Away_Team_Score>3</Away_Team_Score>
   </game>

   <game status="played">
      <Home_Team>D</Home_Team>
      <Away_Team>C</Away_Team>
      <Date>2013-06-19</Date>
      <Home_Team_Score>8</Home_Team_Score>
      <Away_Team_Score>7</Away_Team_Score>
   </game>

   <game status="pending">
      <Home_Team>B</Home_Team>
      <Away_Team>C</Away_Team>
      <Date>2013-07-25</Date>
      <Home_Team_Score>0</Home_Team_Score>
      <Away_Team_Score>0</Away_Team_Score>
   </game>

   <game status="pending">
      <Home_Team>C</Home_Team>
      <Away_Team>D</Away_Team>
      <Date>2013-07-27</Date>
      <Home_Team_Score>0</Home_Team_Score>
      <Away_Team_Score>0</Away_Team_Score>
   </game>
</Schedule>

这是我的xsl代码:

 <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:key name="TeamByName" match="Home_Team|Away_Team" use="."/>
      <xsl:template match="/*">
       <html>
          <body>
     <table border="1">
      <tr>
       <td>Team</td>
       <td>Wins</td>
       <td>Losses</td>
       <td>Tie</td>
      </tr>
      <xsl:for-each select="//game[@status='played']">
      <xsl:apply-templates select="(*/Home_Team | */Away_Team)[generate- id()=generate-  id(key('TeamByName', .)[1])]">
    <xsl:sort select="."/>
<     /xsl:apply-templates>
      </table>
      </body>
      </html>
     </xsl:template> 

      <xsl:template match="Home_Team|Away_Team">
      <tr>
       <td>
        <xsl:value-of select="."/>
       </td>
       <td>

        <xsl:value-of select=
        "count(key('TeamByName', .)
                     [self::Home_Team
                    and
                      ../Home_Team_Score > ../Away_Team_Score 
                    or
                      self::Away_Team
                    and
                      ../Away_Team_Score > ../Home_Team_Score
                     ]
                 )"/>

       </td>
       <td>

        <xsl:value-of select= "count(key('TeamByName', .)
                     [self::Home_Team
                    and
                      ../Away_Team_Score > ../Home_Team_Score
                    or
                      self::Away_Team
                    and
                      ../Home_Team_Score > ../Away_Team_Score
                     ]
                 )"/>

       </td>
       <td>

        <xsl:value-of select="count(key('TeamByName', .)
                     [../Home_Team_Score = ../Away_Team_Score]
                 )"/>

       </td>
      </tr>
     </xsl:template>
    </xsl:stylesheet>

输出应该采用以下形式:

<table border="1">
   <tr>
      <td>Team</td>
      <td>Wins</td>
      <td>Losses</td>
      <td>Ties</td>
   </tr>
   <tr>
      <td>A</td>
      <td>1</td>
      <td>0</td>
      <td>2</td>
   </tr>
   <tr>
      <td>B</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
   </tr>
   <tr>
      <td>C</td>
      <td>0</td>
      <td>2</td>
      <td>1</td>
   </tr>
   <tr>
      <td>D</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
   </tr>
</table>

0 个答案:

没有答案