xslt multiple grouping表格中的格式数据,有些列为空

时间:2013-03-14 16:40:34

标签: xslt grouping xslt-grouping formatting

使用XSLT 1.0,我该如何更改以下内容:

<root>
  <element id="1" Team="Rangers" Season="2011" Points="12" />
  <element id="2" Team="Rangers" Season="2012" Points="5" />
  <element id="3" Team="Rangers" Season="2012" Points="4" />
  <element id="4" Team="Rangers" Season="2013" Points="3" />
  <element id="5" Team="Astros" Season="2011" Points="12" />
  <element id="6" Team="Astros" Season="2013" Points="2" />
  <element id="7" Team="Astros" Season="2013" Points="1" />
  <element id="8" Team="Astros" Season="2013" Points="2" />
</root>

分为:

<table>
    <tr><td>Team</td>
    <td>2011</td>
    <td>2012</td>
    <td>2013</td>
    <td>Total</td></tr>

    <tr><td>Rangers</td>
    <td>12</td>
    <td>9</td>
    <td>3</td>
    <td>20</td></tr>

    <tr><td>Astros</td>
    <td>12</td>
    <td>0</td>
    <td>5</td>
    <td>14</td></tr>
</table>

我在这里问了一个类似的问题,但遗漏了一个重要细节。 xslt Format data in a table some columns are blank 请注意,子分组也有摘要。 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我不完全确定这个问题与你的上一个问题有什么不同,但是应该这样做:

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

  <xsl:key name="kTeam" match="element/@Team" use="."/>
  <xsl:key name="kSeason" match="element/@Season" use="."/>

  <xsl:variable name="distinctSeasons"
                select="/root/element/@Season
                          [generate-id() = generate-id(key('kSeason', .)[1])]" />

  <xsl:template match="root">
    <table>
      <tbody>
        <tr>
          <td>Team</td>
          <xsl:apply-templates select="$distinctSeasons" mode="header">
            <xsl:sort select="." data-type="number" />
          </xsl:apply-templates>
          <td>Total</td>
        </tr>

        <xsl:apply-templates 
            select="element/@Team[generate-id() = 
                                  generate-id(key('kTeam', .)[1])]" />
      </tbody>
    </table>
  </xsl:template>

  <xsl:template match="@Team">
    <xsl:variable name="thisTeamRows" select="key('kTeam', .)/.." />

    <tr>
      <td>
        <xsl:value-of select="." />
      </td>
      <xsl:apply-templates select="$distinctSeasons" mode="total">
        <xsl:sort select="." data-type="number" />
        <xsl:with-param name="teamRows" select="$thisTeamRows" />
      </xsl:apply-templates>
      <td>
        <xsl:value-of select="sum($thisTeamRows/@Points)" />
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="@Season" mode="header">
    <td>
      <xsl:value-of select="." />
    </td>
  </xsl:template>

  <xsl:template match="@Season" mode="total">
    <xsl:param name="teamRows" />
    <td>
      <xsl:value-of select="sum($teamRows[@Season = current()]/@Points)"/>
    </td>
  </xsl:template>
</xsl:stylesheet>

在样本输入上运行时,会产生:

<table>
  <tbody>
    <tr>
      <td>Team</td>
      <td>2011</td>
      <td>2012</td>
      <td>2013</td>
      <td>Total</td>
    </tr>
    <tr>
      <td>Rangers</td>
      <td>12</td>
      <td>9</td>
      <td>3</td>
      <td>24</td>
    </tr>
    <tr>
      <td>Astros</td>
      <td>12</td>
      <td>0</td>
      <td>5</td>
      <td>17</td>
    </tr>
  </tbody>
</table>