Skip to content Skip to sidebar Skip to footer

XSLT Transformation Into Fixed Table Structure

I have a question regarding XSLT transformation for ouput tables with fixed structure and variable input content. I have outlined 2 different example. The desired output table is

Solution 1:

I believe this should do the trick. Note the use of the NoneRows template to fill in the extra cells to make 6:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:key name="kGroup" match="page" use="@section"/>

  <xsl:template match="/*">
    <table>
      <xsl:variable name="groups"
         select="*[generate-id() = generate-id(key('kGroup', @section)[1])]" />

      <tr>
        <xsl:apply-templates select="$groups" mode="top" />
        <xsl:call-template name="NoneCells">
          <xsl:with-param name="count" select="6 - count($groups)" />
        </xsl:call-template>
      </tr>
      <tr>
        <xsl:apply-templates select="$groups" mode="pageNums" />
        <xsl:call-template name="NoneCells">
          <xsl:with-param name="count" select="6 - count($groups)" />
        </xsl:call-template>
      </tr>
    </table>
  </xsl:template>

  <xsl:template match="page" mode="top">
    <xsl:variable name="sectName" select="substring-after(@section, 'Arsenal_')" />
    <td class="{$sectName}">
      <xsl:value-of select="$sectName" />
    </td>
  </xsl:template>

  <xsl:template match="page" mode="pageNums">
    <xsl:variable name="groupMembers" select="key('kGroup', @section)" />
    <td class="{substring-after(@section, 'Arsenal_')}_R2">
      <xsl:value-of select="concat($groupMembers[1]/@number, '-', 
                                   $groupMembers[last()]/@number)"/>
    </td>
  </xsl:template>

  <xsl:template name="NoneCells">
    <xsl:param name="count" />
    <xsl:if test="$count > 0">
      <td class="None"></td>
      <xsl:call-template name="NoneCells">
        <xsl:with-param name="count" select="$count - 1" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

When run on your first sample input:

<table>
  <tr>
    <td class="Stadium">Stadium</td>
    <td class="Crowds">Crowds</td>
    <td class="Support">Support</td>
    <td class="Revenue">Revenue</td>
    <td class="Cost">Cost</td>
    <td class="Outlook">Outlook</td>
  </tr>
  <tr>
    <td class="Stadium_R2">1-4</td>
    <td class="Crowds_R2">5-8</td>
    <td class="Support_R2">9-12</td>
    <td class="Revenue_R2">13-16</td>
    <td class="Cost_R2">17-20</td>
    <td class="Outlook_R2">21-24</td>
  </tr>
</table>

When run on your second sample input:

<table>
  <tr>
    <td class="Stadium">Stadium</td>
    <td class="Support">Support</td>
    <td class="Cost">Cost</td>
    <td class="Outlook">Outlook</td>
    <td class="None" />
    <td class="None" />
  </tr>
  <tr>
    <td class="Stadium_R2">1-4</td>
    <td class="Support_R2">5-8</td>
    <td class="Cost_R2">9-12</td>
    <td class="Outlook_R2">13-16</td>
    <td class="None" />
    <td class="None" />
  </tr>
</table>

Post a Comment for "XSLT Transformation Into Fixed Table Structure"