【问题标题】:Having trouble displaying XML information through XSL in a table在表格中通过 XSL 显示 XML 信息时遇到问题
【发布时间】:2014-02-11 22:53:39
【问题描述】:

我正在尝试以表格格式显示静态信息或使用函数计算的信息。

基本上我有几场比赛的一些球队统计数据,有一支主队和一支客队,只有状态为“已参加”的比赛才应计入比分,但我需要将它们显示在按以下排名的表格中他们的胜利。

这里是 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>

这是我试图显示表格的 XSL

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

我什至无法显示表格。我可以将记分板模板中的代码直接放在正文中,它会显示,但不是调用模板时的方式。

然后我不确定如何在我的 for-each 循环中计算输赢等

【问题讨论】:

  • 看起来你的 XML 在中间被切断了,只在一场比赛之后并且没有关闭 &lt;Schedule&gt; 标签。你能发布一个更完整的例子吗?
  • 我刚刚发布了一个完整的“游戏”元素以缩短篇幅。它只是不断重复不同的“游戏”元素,并以 标签结束。
  • 嗯,很高兴看到更多,所以我们可以有一些东西来测试。

标签: xml xslt


【解决方案1】:

在我看来,您好像在这里混淆了“模板匹配”和“模板名称”。您正在匹配源中不存在的元素标题和记分牌,但您想要做的似乎是将它们用作称为模板。我认为您应该阅读有关如何在 XSLT 中使用模板以及匹配和名称模板之间的区别...

另外很奇怪的是,当你调用 calcscores 时,它应该怎么做?目前这将为每个团队生成一个 h1 元素,在一个 tr 内......似乎没有意义,我猜你想为每个团队生成一个 tr

试试这个,也许这会帮助你开始:

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

【讨论】:

  • 谢谢,这绝对让我前进,我想我可以从那里计算出很多计算,除了应该按获胜次数的排名,因为这不是在 for-每个,如何排序?
  • 另外,除了“状态”为“已玩”的游戏外,我如何排除所有游戏?
  • 只需使用 //Game[@status='Played'] 仅获取已玩过的游戏,对于排名我不确定您是否甚至可以使用 XSLT 1 一步完成此操作,因为在计算排名之前,您需要获胜次数...您使用什么来进行转换?您可以使用支持 XSLT 2 的处理器吗?这会让事情变得更容易
  • 我可以使用任何东西,javascript、DOM、CSS、XSLT,但我确实需要在没有基于服务器的处理的情况下进行。
  • 好吧,我建议您使用 XSLT 生成除排名以外的所有内容,并尝试使用计算排名的 javascript 函数...如果您愿意,可以看看如何使表格可排序而不是按胜利排序,你基本上得到了排名......如果你这样做,你应该为此打开另一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 2017-11-11
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多