[xsl] structuring templates to generate html tables

Subject: [xsl] structuring templates to generate html tables
From: "Andrew Welch" <awelch@xxxxxxxxxxxxxxx>
Date: Tue, 23 Apr 2002 17:43:31 +0100
Hi list,

I have a problem with trying to create a table from nested data.  The data
is nested as follows:

<root>
  <para0>
    <para>apple</para>
    <subpara1>
      <para>orange</para>
      <subpara2>
        <para>bannana</para>
      </subpara2>
    </subpara1>
  </para0>
</root>

The output I need is like this:

<table>
<tr><td>1</td><td>apple</td></tr>
<tr><td>2</td><td>orange</td></tr>
<tr><td>3</td><td>bannana</td></tr>
</table>

Simple enough so far, but para0, subpara1 etc can contain any nodes.  For
example, this xsl will give me the output I need above:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="root/para0">
<table>
  <xsl:apply-templates/>
</table>
</xsl:template>

<xsl:template match="subpara1|subpara2">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="para">
  <tr>
    <td><xsl:value-of select="count(preceding::para|.)"/></td>
    <td><xsl:apply-templates/></td>
  </tr>
</xsl:template>
</xsl:stylesheet>

I could have a <randlist> element in my example above, with the template for
it looking something like this:

<xsl:template match="randlist">
  <ul>
  <xsl:for-each select="item">
    <li><xsl:apply-templates/></li>
  </xsl:for-each>
  </ul>
</xsl:template>

This element can occur within any element (just like <para>), but for it to
work with the example above it would need to be wrapped in <tr><td>'s.  I
have 100's of elements to deal with, so having two templates for each
element (ie one for within table, one for outside) isn't an option.  How can
I structure my templates above to allow any element in the table (with the
template working just as well outside of the table)?


Apologies to anyone who doesn't understand my poor explanation, but
hopefully someone will ;)

cheers
andrew







************************************************************************
*<a href="http://www.thebristoldirectory.com"; >The Bristol Directory</a>*
************************************************************************

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.350 / Virus Database: 196 - Release Date: 17/04/2002



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


Current Thread