0

With XSLT 1.0, how can I change the following:

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

Into:

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

I asked a similar question here but missed an important detail. xslt Format data in a table some columns are blank Notice that the sub groupings also has a summary. Any help would be greatly appreciated!

Community
  • 1
  • 1
  • Before moving onto a new question, please remember to [accept answers](http://stackoverflow.com/faq#howtoask) to your questions that have already been answered. – JLRishe Mar 14 '13 at 17:24
  • I don't understand the totals in your example output. Why is the first row 20 when 12+9+3=24, and why is the second 14, when 12+0+5=14? – JLRishe Mar 14 '13 at 17:36

1 Answers1

0

I'm not entirely sure how this question is different from your last one, but this should do it:

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

When run on your sample input, this produces:

<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>
JLRishe
  • 99,490
  • 19
  • 131
  • 169