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:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:outputmethod="xml"indent="yes"omit-xml-declaration="yes"/><xsl:keyname="kGroup"match="page"use="@section"/><xsl:templatematch="/*"><table><xsl:variablename="groups"select="*[generate-id() = generate-id(key('kGroup', @section)[1])]" /><tr><xsl:apply-templatesselect="$groups"mode="top" /><xsl:call-templatename="NoneCells"><xsl:with-paramname="count"select="6 - count($groups)" /></xsl:call-template></tr><tr><xsl:apply-templatesselect="$groups"mode="pageNums" /><xsl:call-templatename="NoneCells"><xsl:with-paramname="count"select="6 - count($groups)" /></xsl:call-template></tr></table></xsl:template><xsl:templatematch="page"mode="top"><xsl:variablename="sectName"select="substring-after(@section, 'Arsenal_')" /><tdclass="{$sectName}"><xsl:value-ofselect="$sectName" /></td></xsl:template><xsl:templatematch="page"mode="pageNums"><xsl:variablename="groupMembers"select="key('kGroup', @section)" /><tdclass="{substring-after(@section, 'Arsenal_')}_R2"><xsl:value-ofselect="concat($groupMembers[1]/@number, '-', 
                                   $groupMembers[last()]/@number)"/></td></xsl:template><xsl:templatename="NoneCells"><xsl:paramname="count" /><xsl:iftest="$count > 0"><tdclass="None"></td><xsl:call-templatename="NoneCells"><xsl:with-paramname="count"select="$count - 1" /></xsl:call-template></xsl:if></xsl:template></xsl:stylesheet>

When run on your first sample input:

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

When run on your second sample input:

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

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