programmerNOOB
programmerNOOB

Reputation: 123

Having trouble displaying XML information through XSL in a table

I am trying to display information whether static or calculated with functions in a table format.

Basically I have some team stats for several games were there is a home team and visiting team and only the games with a status of "played" should be figured into the scores, but I need to display them in a table ranked by their wins.

Here is the XML

    <Schedule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Teams>
            <Team>Blue Jays</Team>
        </Teams>



        <Game> 
            <Home_Team>Blue Jays</Home_Team>
            <Away_Team>Marlins</Away_Team>
            <Date>2012-01-10</Date>
            <Home_Team_Score>7</Home_Team_Score>
            <Away_Team_Score>9</Away_Team_Score>
        </Game>

Here is the XSL that I am trying to get to display the table

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="4.0"/>
    <xsl:key name="team" match="Teams" use="Team"/>

    <xsl:template match="/Schedule">
        <html>
            <head>
                <title><xsl:value-of select="League"/>
                </title>
                <link href="batty.css" rel="stylesheet" type="text/css"/>
            </head>
            <body>
                <xsl:apply-templates select="headliner"/>
            </body>
        </html>
    </xsl:template>


    <xsl:template match="headliner">
        <h1>
            <xsl:value-of select="League"/>
        </h1>
        <h5>
            <th>put date here</th>
        </h5>
        <xsl:apply-templates select="scoreboard"/>
    </xsl:template>   
<xsl:template match="scoreboard">
    <table cellspacing="1" cellpadding="2" id="scores">
        <tr class="title">
            <th colspan="22">Season <xsl:value-of select="//Schedule[@season]"/></th>
        </tr>

        <tr class="fields">
            <th style="text-align: left">Team</th>
            <th>Rank</th>
            <th>Wins</th>
            <th>Losses</th>
            <th>Ties</th>
            <th>Points Earned</th>
            <th>Points Against</th>
            <th>Win %</th>
            <th>Games Behind</th>
        </tr>
        <tr class="rankingTeams">
            <xsl:call-template name="calcScores">
            </xsl:call-template>
        </tr>

    </table>
</xsl:template>
<xsl:template name="calcScores">
    <xsl:variable name="wins" />
    <xsl:variable name="losses" />
    <xsl:variable name="ties" />
    <xsl:variable name="pointsEarned" />
    <xsl:variable name="winPercentage" />
    <xsl:variable name="gamesBehind" />
    <xsl:for-each
        select="//Teams[generate-id()=generate-id(key('team', Team)[1])]">
        <xsl:sort select="Team" />
        <h1><xsl:value-of select="Team" /></h1>
    </xsl:for-each>
</xsl:template> 
    </xsl:stylesheet>

I am having trouble with the table even showing. I can place the code from scoreboard template directly in the body and it will show, but not the way it is here when calling the template.

Then I am not sure how to go about calculating the wins and losses and so forth in my for-each loop

Upvotes: 0

Views: 186

Answers (1)

Tobias Klevenz
Tobias Klevenz

Reputation: 1645

It looks to me as if you are mixing up "template match" and "template name" here. You are matching the elements headliner and scoreboard which are not present in your source, but what you want to do seems like use those as a called template. I think you should read up on how to use templates in XSLT and what is the difference between match and name templates...

Also very strange is when you call calcscores, what should that do? Currently this would produce a h1 element for each team, inside a tr...does not seem as if it makes sense, I guess you want to generate a tr for each team.

Try this, maybe this will help you get started:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0"/>
<xsl:key name="team" match="Teams" use="Team"/>

<xsl:template match="/Schedule">
    <html>
        <head>
            <title>
                <xsl:value-of select="League"/>
            </title>
            <link href="batty.css" rel="stylesheet" type="text/css"/>
        </head>
        <body>
            <xsl:call-template name="headliner"/>
            <xsl:call-template name="scoreboard"/>
        </body>
    </html>
</xsl:template>

<xsl:template name="headliner">
    <h1>
        <xsl:value-of select="League"/>
    </h1>
    <h5>
        <th>put date here</th>
    </h5>
    <xsl:apply-templates select="scoreboard"/>
</xsl:template>

<xsl:template name="scoreboard">
    <table cellspacing="1" cellpadding="2" id="scores">
        <tr class="title">
            <th colspan="22">Season <xsl:value-of select="//Schedule/@season"/></th>
        </tr>

        <tr class="fields">
            <th style="text-align: left">Team</th>
            <th>Rank</th>
            <th>Wins</th>
            <th>Losses</th>
            <th>Ties</th>
            <th>Points Earned</th>
            <th>Points Against</th>
            <th>Win %</th>
            <th>Games Behind</th>
        </tr>
        <xsl:apply-templates select="//Teams">
            <xsl:sort select="Team"/>
        </xsl:apply-templates>
    </table>
</xsl:template>

<xsl:template match="Team">
    <tr class="rankingTeams">
        <td style="text-align: left">
            <xsl:value-of select="."/>
        </td>
        <td><!-- Rank: no idea how you want to calculate this --></td>
        <td>
            <!-- Wins -->
            <xsl:variable name="homeWins"
                select="count(//Game[Home_Team=current()][Home_Team_Score &gt; Away_Team_Score])"
            />
            <xsl:variable name="awayWins"
                select="count(//Game[Away_Team=current()][Away_Team_Score &gt; Home_Team_Score])"
            />
            <xsl:value-of select="$homeWins + $awayWins"/>
        </td>
        <td><!-- Loses: similar to Wins --></td>
        <td><!-- Ties: similar to Wins --></td>
        <td><!-- Points Earned --></td>
        <td><!-- Points Against --></td>
        <td><!-- Win % --></td>
        <td><!-- Games Behind --></td>
    </tr>

</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions