xml - selecting nodes based on the attribute value -
i have been struggling days determine how take xml file of game results (teams , final scores) , generate team standings list shows each team along how many times won, lost or tied based on game status(played). able display team standings on both game status(played , pending). want display team standings played only.any appreciated.
here xml code:
<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>
here xsl code:
<?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 , ../home_team_score > ../away_team_score or self::away_team , ../away_team_score > ../home_team_score ] )"/> </td> <td> <xsl:value-of select= "count(key('teambyname', .) [self::home_team , ../away_team_score > ../home_team_score or self::away_team , ../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>
output should in form of:
<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>
Comments
Post a Comment