Re: [xsl] Inserting leaf node names as headers in a table

Subject: Re: [xsl] Inserting leaf node names as headers in a table
From: "cking" <cking@xxxxxxxxxx>
Date: Tue, 14 Sep 2004 08:30:45 +0200
Hemanth,

Use a template mode to generate the header rows:

<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></title>
   </head>
   <body>
    <xsl:apply-templates/>
   </body>
  </html>
 </xsl:template>

 <xsl:template match="A/B">
  <table border="1">
   <thead>
    <xsl:apply-templates select="D[1]" mode="header"/>
   </thead>
   <tbody>
    <xsl:apply-templates select="D"/>
   </tbody>
  </table>
 </xsl:template>

 <xsl:template match="D" mode="header">
  <tr>
   <xsl:apply-templates select="E/* | F/* | G/GG/*" mode="header"/>
  </tr>
 </xsl:template>

 <xsl:template match="D">
  <tr>
   <xsl:apply-templates select="E/* | F/* | G/GG/*"/>
  </tr>
 </xsl:template>

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

 <xsl:template match="E/* | F/* | G/GG/*">
  <td>
   <xsl:value-of select="."/>
  </td>
 </xsl:template>

</xsl:stylesheet>


Cheers,
Anton Triest


Hemanth Singamsetty wrote:
>

> I'm trying to insert the leaf node tagnames, of a XML file, as header values in a table using XSLT.
> My XML file looks like this:
>
> <A>
>    <B>
>       <D>
>         <E>
>           <E1>e1</E1>
>           <E2>e2</E2>
>           <E3>e3</E3>
>             :
>          </E>
>         <F>
>           <F1>f1</F1>
>           <F2>f2</F2>
>           <F3>f3</F3>
>             :
>         </F>
>         <G>
>           <GG>
>             <GGG>ggg1</GGG>
>             <GGG>ggg2</GGG>
>           </GG>
>           <GG>
>             <GGG>ggg3<GGG>
>             <GGG>ggg4</GGG>
>           </GG>
>             :
>             :
>         </G>
>       </D>
>       <D>
>       <D>
>          :
>          :
>    <B>
>    <B>
>      :
>      :
> </A>
>
>
> The output I'm trying to get is a table with different columns having headers as
> (E1, E2, E3....,F1, F2, F3,...., GGG1, GGG2, GGG3, GGG4....) and the values inside
> the table corresponding to the above header as
> (e1, e2, e3,....,f1, f2, f3....ggg1, ggg2, ggg3, ggg4...and so on for each <D>)
>
>
> I was able to create and populate the table with values using <xsl: apply-templates> but
> I'm new to XML and XSL programming and I'm finding it difficult to display the header values
> only once, i mean, i'm trying to put a <xsl:if> condition in a <xsl:for-each> to stop after
> one iteration.
>
> I'm doing something like this in the <xsl:template match="B">, but it didn't work.
>
> <tr>
> <xsl:for-each select="????">
>    <xsl:if test="position() &lt; count("????")">
>       <th><xsl:value-of select="local-name()"/></th>
>    </xsl:if>
> </xsl:for-each>
> </tr>
>
>
> Hope my question is clear. Can someone help me figure out how should I do this?
>
>
>
> Thanks in advance,
> Hemanth

Current Thread