RE: [xsl] Create table with optional elements

Subject: RE: [xsl] Create table with optional elements
From: "Martin Rowlinson \(MarrowSoft\)" <marrow@xxxxxxxxxxxxxx>
Date: Tue, 27 May 2003 12:15:17 +0100
Hi Ricardo,

Assuming that you have your namespaces declared, e.g.

<wn2:ArrayOfToken_Response i:type="wn1:ArrayOfToken"
  xmlns:wn2="namespace-wn2" xmlns:wn1="namespace-wn1"
xmlns:i="namespace-i">
  ....

then try something like...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:wn2="namespace-wn2" xmlns:wn1="namespace-wn1"
xmlns:i="namespace-i">
<xsl:output method="html" indent="yes"/>
<!-- key for finding distinct columns -->
<xsl:key name="kDistinctCols" match="wn1:lang" use="."/>
<!-- find distinct columns here so that they can be re-used in different
templates -->
<xsl:variable name="DistinctCols"
select="/wn2:ArrayOfToken_Response/wn1:Token/wn1:term/wn1:Term/wn1:lang[
generate-id() = generate-id(key('kDistinctCols',.))]"/>
<xsl:template match="wn2:ArrayOfToken_Response">
  <html>
    <body>
      <table border="1">
        <!-- header row -->
        <tr>
          <xsl:for-each select="$DistinctCols">
            <xsl:sort/>
            <th>
              <xsl:value-of select="."/>
            </th>
          </xsl:for-each>
        </tr>
        <!-- data rows -->
        <xsl:apply-templates select="wn1:Token"/>
      </table>
    </body>
  </html>
</xsl:template>

<xsl:template match="wn1:Token">
  <xsl:variable name="this-terms" select="wn1:term/wn1:Term"/>
  <tr>
    <xsl:for-each select="$DistinctCols">
      <xsl:sort/>
      <xsl:variable name="this-cell" select="$this-terms[wn1:lang =
current()]"/>
      <td>
        <xsl:choose>
          <xsl:when test="$this-cell">
            <xsl:value-of select="$this-cell/wn1:data"/>
          </xsl:when>
          <xsl:otherwise>
            <!-- do whatever you want to indicate no cell content -->
            <xsl:text>[none!]</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </td>
    </xsl:for-each>
  </tr>
</xsl:template>
</xsl:stylesheet>


Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator




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


Current Thread