[xsl] Display table where colomns are variable

Subject: [xsl] Display table where colomns are variable
From: "Tim Watts" <timw@xxxxxxx>
Date: Tue, 13 Mar 2001 15:20:21 +1100
Welcome to today's tiny puzzle...

I would like to use a standard template to display data throughout a site,
but the number of columns required will be different depending on the
various pieces of data required to be displayed.

Each XSL would call an external document which contains the

This would use something simular to following to select the child element
which the rows are created for...

...
<table>
<xsl:apply-template select="root/parent/child" />
<table>
...

and for each root/parent/child node the row is created with a template which
should call the showdata template(external shared template) for each of the
select nodes for which we want to display the data.

<xsl:template match="root/parent/child">
  <tr>
  <xsl:call-template name="showdata">
    <xsl:with-param name="column1"><xsl:value-of
select="firstname"><xsl:text> </xsl:text><xsl:value-of
select="lastname"/></xsl:with-param>
    <xsl:with-param name="column2" select "otherthing" />
    <xsl:with-param name="column3" select "someotherthing" />
    <xsl:with-param name="column4" select "somethingelse" />
    ...
    ...
  </xsl:call-template>
  </tr>
</xsl:template>

which calls the following template "showdata" in an external document...

<xsl:template name="showdata">
    <xsl:param name="column1">Null</xsl:param>
    <xsl:param name="column2">Null</xsl:param>
    <xsl:param name="column3">Null</xsl:param>
    <xsl:param name="column4">Null</xsl:param>
    ...
    ...
    <xsl:if test="not($column1='Null')"><td><xsl:value-of select="$column1"
/></td></xsl:if>
    <xsl:if test="not($column2='Null')"><td><xsl:value-of select="$column2"
/></td></xsl:if>
    <xsl:if test="not($column3='Null')"><td><xsl:value-of select="$column3"
/></td></xsl:if>
    <xsl:if test="not($column4='Null')"><td><xsl:value-of select="$column4"
/></td></xsl:if>
...
...
</xsl:template>

There are some problems with this approach -
One, it is long-winded
Two, by using "Null" as a test if a value for that param was passed, if you
want the value to say "Null" it won't work
Three, there will have to be a max no. of columns (however many column
params that are allowed for) - although practically, you will probably want
to set a max anyway.

Could someone come up with a better solution than the one devised here? I
would really appreciate it.

Thanks,

Tim Watts


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread