使用 XSLT muenchian 分组来计算运动队排名(胜/负)?

发布于 2025-01-08 17:53:44 字数 2745 浏览 1 评论 0原文

几天来我一直在努力确定如何获取比赛结果(球队和最终得分)的 XML 文件并生成球队排名列表,该列表显示每个球队以及他们获胜、失败或平局的次数。结果也应该按总胜利排序,但我什至无法找出计算胜/负的好方法,更不用说按结果排序了。我知道它必须涉及慕尼黑分组,并且我已经编写了找到所有不同团队的部分代码,但我不知道从哪里开始。任何帮助将不胜感激。

games.xml

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="games.xsl"?>

<Games>

  <Game>
    <Home>Team A</Home>
    <Away>Team B</Away>
    <Home_Score>20</Home_Score>
    <Away_Score>15</Away_Score>
  </Game>

  <Game>
    <Home>Team C</Home>
    <Away>Team D</Away>
    <Home_Score>12</Home_Score>
    <Away_Score>18</Away_Score>
  </Game>

  <Game>
    <Home>Team A</Home>
    <Away>Team C</Away>
    <Home_Score>8</Home_Score>
    <Away_Score>8</Away_Score>
  </Game>

  <Game>
    <Home>Team B</Home>
    <Away>Team D</Away>
    <Home_Score>6</Home_Score>
    <Away_Score>14</Away_Score>
  </Game>

  <Game>
    <Home>Team D</Home>
    <Away>Team C</Away>
    <Home_Score>9</Home_Score>
    <Away_Score>11</Away_Score>
  </Game>

  <Game>
    <Home>Team C</Home>
    <Away>Team A</Away>
    <Home_Score>13</Home_Score>
    <Away_Score>13</Away_Score>
  </Game>

</Games>

games.xsl

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

  <!-- Key for identifying teams -->
  <xsl:key name="unique-teams" match="/Games/Game" use="Home" />

  <xsl:template match="/">
    <html>
    <head>
    <title>Team Standings</title>
    </head>
    <body>

        <!-- Get distinct teams, sort by team name -->
        <xsl:apply-templates select="//Game[generate-id() = generate-id(key('unique-teams', Home)[1])]">
            <xsl:sort select="Home" />
        </xsl:apply-templates>

    </body>
    </html>
  </xsl:template>

  <xsl:template match="Game">

    <!-- Current team -->
    <xsl:variable name="selectedteam" select="Home" />

    <!-- Output each unique team name to the screen -->
    <h1><xsl:value-of select="$selectedteam"/></h1>

    <!-- Loop through all games to calculate totals??? -->
    <xsl:for-each select="//Game">    
    </xsl:for-each>

  </xsl:template>

</xsl:stylesheet>

I have been struggling for days to determine how to take an XML file of game results (teams and final scores) and generate a team standings list that shows each team along with how many times they won, lost or tied. The results should also be sorted by total wins, but I can't even figure out a good method of calculating the wins/losses let alone sorting by the results. I know it must involve muenchian grouping, and I've coded the part that finds all distinct teams but am stumped where to go from there. Any help would be GREATLY appreciated.

games.xml

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="games.xsl"?>

<Games>

  <Game>
    <Home>Team A</Home>
    <Away>Team B</Away>
    <Home_Score>20</Home_Score>
    <Away_Score>15</Away_Score>
  </Game>

  <Game>
    <Home>Team C</Home>
    <Away>Team D</Away>
    <Home_Score>12</Home_Score>
    <Away_Score>18</Away_Score>
  </Game>

  <Game>
    <Home>Team A</Home>
    <Away>Team C</Away>
    <Home_Score>8</Home_Score>
    <Away_Score>8</Away_Score>
  </Game>

  <Game>
    <Home>Team B</Home>
    <Away>Team D</Away>
    <Home_Score>6</Home_Score>
    <Away_Score>14</Away_Score>
  </Game>

  <Game>
    <Home>Team D</Home>
    <Away>Team C</Away>
    <Home_Score>9</Home_Score>
    <Away_Score>11</Away_Score>
  </Game>

  <Game>
    <Home>Team C</Home>
    <Away>Team A</Away>
    <Home_Score>13</Home_Score>
    <Away_Score>13</Away_Score>
  </Game>

</Games>

games.xsl

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

  <!-- Key for identifying teams -->
  <xsl:key name="unique-teams" match="/Games/Game" use="Home" />

  <xsl:template match="/">
    <html>
    <head>
    <title>Team Standings</title>
    </head>
    <body>

        <!-- Get distinct teams, sort by team name -->
        <xsl:apply-templates select="//Game[generate-id() = generate-id(key('unique-teams', Home)[1])]">
            <xsl:sort select="Home" />
        </xsl:apply-templates>

    </body>
    </html>
  </xsl:template>

  <xsl:template match="Game">

    <!-- Current team -->
    <xsl:variable name="selectedteam" select="Home" />

    <!-- Output each unique team name to the screen -->
    <h1><xsl:value-of select="$selectedteam"/></h1>

    <!-- Loop through all games to calculate totals??? -->
    <xsl:for-each select="//Game">    
    </xsl:for-each>

  </xsl:template>

</xsl:stylesheet>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人骨 2025-01-15 17:53:44

您想要这样的结果 (XSLT 1.0):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kTeamByName" match="Home|Away" use="."/>

 <xsl:template match="/*">

 <table border="1">
  <tr>
   <td>Team</td><td>W</td><td>D</td><td>L</td>
  </tr>
      <xsl:apply-templates select=
      "(*/Home | */Away)
        [generate-id()
        =
         generate-id(key('kTeamByName', .)[1])
        ]
      ">
      <xsl:sort data-type="number" order="descending" select=
       "count(key('kTeamByName', .)
                 [self::Home
                and
                  ../Home_Score > ../Away_Score
                or
                  self::Away
                and
                  ../Away_Score > ../Home_Score
                 ]
             )
       "/>

      </xsl:apply-templates>
  </table>
 </xsl:template>

 <xsl:template match="Home|Away">
  <tr>
   <td>
    <xsl:value-of select="."/>
   </td>
   <td>
    <xsl:value-of select=
    "count(key('kTeamByName', .)
                 [self::Home
                and
                  ../Home_Score > ../Away_Score
                or
                  self::Away
                and
                  ../Away_Score > ../Home_Score
                 ]
             )"/>
   </td>
   <td>
    <xsl:value-of select=
    "count(key('kTeamByName', .)
                 [../Home_Score = ../Away_Score]
             )"/>
   </td>
   <td>
    <xsl:value-of select=
    "count(key('kTeamByName', .)
                 [self::Home
                and
                  ../Away_Score > ../Home_Score
                or
                  self::Away
                and
                  ../Home_Score > ../Away_Score
                 ]
             )"/>
   </td>
  </tr>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时

<Games>
    <Game>
        <Home>Team A</Home>
        <Away>Team B</Away>
        <Home_Score>20</Home_Score>
        <Away_Score>15</Away_Score>
    </Game>
    <Game>
        <Home>Team C</Home>
        <Away>Team D</Away>
        <Home_Score>12</Home_Score>
        <Away_Score>18</Away_Score>
    </Game>
    <Game>
        <Home>Team A</Home>
        <Away>Team C</Away>
        <Home_Score>8</Home_Score>
        <Away_Score>8</Away_Score>
    </Game>
    <Game>
        <Home>Team B</Home>
        <Away>Team D</Away>
        <Home_Score>6</Home_Score>
        <Away_Score>14</Away_Score>
    </Game>
    <Game>
        <Home>Team D</Home>
        <Away>Team C</Away>
        <Home_Score>9</Home_Score>
        <Away_Score>11</Away_Score>
    </Game>
    <Game>
        <Home>Team C</Home>
        <Away>Team A</Away>
        <Home_Score>13</Home_Score>
        <Away_Score>13</Away_Score>
    </Game>
</Games>

生成所需的正确结果

<table border="1">
   <tr>
      <td>Team</td>
      <td>W</td>
      <td>D</td>
      <td>L</td>
   </tr>
   <tr>
      <td>Team D</td>
      <td>2</td>
      <td>0</td>
      <td>1</td>
   </tr>
   <tr>
      <td>Team A</td>
      <td>1</td>
      <td>2</td>
      <td>0</td>
   </tr>
   <tr>
      <td>Team C</td>
      <td>1</td>
      <td>2</td>
      <td>1</td>
   </tr>
   <tr>
      <td>Team B</td>
      <td>0</td>
      <td>0</td>
      <td>2</td>
   </tr>
</table>

You want something like this (XSLT 1.0):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kTeamByName" match="Home|Away" use="."/>

 <xsl:template match="/*">

 <table border="1">
  <tr>
   <td>Team</td><td>W</td><td>D</td><td>L</td>
  </tr>
      <xsl:apply-templates select=
      "(*/Home | */Away)
        [generate-id()
        =
         generate-id(key('kTeamByName', .)[1])
        ]
      ">
      <xsl:sort data-type="number" order="descending" select=
       "count(key('kTeamByName', .)
                 [self::Home
                and
                  ../Home_Score > ../Away_Score
                or
                  self::Away
                and
                  ../Away_Score > ../Home_Score
                 ]
             )
       "/>

      </xsl:apply-templates>
  </table>
 </xsl:template>

 <xsl:template match="Home|Away">
  <tr>
   <td>
    <xsl:value-of select="."/>
   </td>
   <td>
    <xsl:value-of select=
    "count(key('kTeamByName', .)
                 [self::Home
                and
                  ../Home_Score > ../Away_Score
                or
                  self::Away
                and
                  ../Away_Score > ../Home_Score
                 ]
             )"/>
   </td>
   <td>
    <xsl:value-of select=
    "count(key('kTeamByName', .)
                 [../Home_Score = ../Away_Score]
             )"/>
   </td>
   <td>
    <xsl:value-of select=
    "count(key('kTeamByName', .)
                 [self::Home
                and
                  ../Away_Score > ../Home_Score
                or
                  self::Away
                and
                  ../Home_Score > ../Away_Score
                 ]
             )"/>
   </td>
  </tr>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<Games>
    <Game>
        <Home>Team A</Home>
        <Away>Team B</Away>
        <Home_Score>20</Home_Score>
        <Away_Score>15</Away_Score>
    </Game>
    <Game>
        <Home>Team C</Home>
        <Away>Team D</Away>
        <Home_Score>12</Home_Score>
        <Away_Score>18</Away_Score>
    </Game>
    <Game>
        <Home>Team A</Home>
        <Away>Team C</Away>
        <Home_Score>8</Home_Score>
        <Away_Score>8</Away_Score>
    </Game>
    <Game>
        <Home>Team B</Home>
        <Away>Team D</Away>
        <Home_Score>6</Home_Score>
        <Away_Score>14</Away_Score>
    </Game>
    <Game>
        <Home>Team D</Home>
        <Away>Team C</Away>
        <Home_Score>9</Home_Score>
        <Away_Score>11</Away_Score>
    </Game>
    <Game>
        <Home>Team C</Home>
        <Away>Team A</Away>
        <Home_Score>13</Home_Score>
        <Away_Score>13</Away_Score>
    </Game>
</Games>

the wanted, correct result is produced:

<table border="1">
   <tr>
      <td>Team</td>
      <td>W</td>
      <td>D</td>
      <td>L</td>
   </tr>
   <tr>
      <td>Team D</td>
      <td>2</td>
      <td>0</td>
      <td>1</td>
   </tr>
   <tr>
      <td>Team A</td>
      <td>1</td>
      <td>2</td>
      <td>0</td>
   </tr>
   <tr>
      <td>Team C</td>
      <td>1</td>
      <td>2</td>
      <td>1</td>
   </tr>
   <tr>
      <td>Team B</td>
      <td>0</td>
      <td>0</td>
      <td>2</td>
   </tr>
</table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文