xsl-list
[Top] [All Lists]

Re: xsl template for simple data-base to transform into html displayable table

2004-09-02 23:45:11
Bovy, Stephen J wrote:


Wow, this is great, I really like it,

Now is their a way to generate the table column headings with out
hard-coding the element names ?? 

is there a value of element-name ????


The name of an element can be obtained with the XPath function 
local-name(). There's also the name() function, returning the
name with its namespace identifier.

Here's a "generic" stylesheet - it will transform any input file
that has the same structure as yours, no matter what the element
names are and no matter how many columns:

<root>
    <row-1>
        <col-A>...</col-A>
        <col-B>...</col-B>
        ...
    </row-1>
    ...
</root>

I changed the templates' select and match attributes to
match all element names ("/*/*/*" means: any element at the 
third level from the root)

Here's some good starting points for learning XSLT/XPath basics:
http://www.w3schools.com/xsl/default.asp
http://www.zvon.org/o_html/group_xsl.html


Cheers
Anton Triest


<?xml version="1.0" encoding="UTF-8"?>

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


 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"
  doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
  />

 <xsl:template match="/">
  <html xmlns="http://www.w3.org/1999/xhtml";>
   <head>
    <title>
     <xsl:value-of select="local-name(/*)"/>
    </title>
   </head>
   <body>
    <xsl:apply-templates/>
   </body>
  </html>
 </xsl:template>

 <xsl:template match="/*">
  <table border="1">
   <tr>
    <xsl:apply-templates select="/*/*[1]/*" mode="header"/>
   </tr>
   <xsl:apply-templates/>
  </table>
 </xsl:template>

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

 <xsl:template match="/*/*/*" mode="header">
  <th><xsl:value-of select="local-name()"/></th>
 </xsl:template>

 <xsl:template match="/*/*/*">
  <td><xsl:value-of select="."/></td>
 </xsl:template>

</xsl:stylesheet>