Re: [xsl] table header respecting xml structure

Subject: Re: [xsl] table header respecting xml structure
From: "Rui Alberto L." Gonçalves <rui-l-goncalves@xxxxxxxxxxxxx>
Date: 05 Feb 2004 16:17:36 +0000
Hi,
here's the solution ... may help if someone needs
to perform a similar  task.
Thanks Manolis for pointing me the right direction!!

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.1">
  <xsl:output method="html"/>
                                                                                                                             
  <xsl:template match="root">
    <table border="1">
      <xsl:variable name="tree-depth">
        <xsl:for-each select="//*[not(*)]">
          <xsl:sort select="count(ancestor::*)"/>
          <xsl:if test="position()=last()">
            <xsl:value-of select="count(ancestor::*)"/>
          </xsl:if>
        </xsl:for-each>
      </xsl:variable>
                                                                                                                             
      <xsl:call-template name="header">
        <xsl:with-param name="nodes" select="./*"/>
        <xsl:with-param name="tree-depth" select="$tree-depth"/>
      </xsl:call-template>
    </table>
  </xsl:template>
                                                                                                                             
                                                                                                                             
  <xsl:template name="header">
    <xsl:param name="nodes" select="."/>
    <xsl:param name="seen" select="/.."/>
    <xsl:param name="tree-depth" select="0"/>
    <xsl:param name="recursion" select="0"/>
      <tr>
      <xsl:for-each select="$nodes">
        <xsl:choose>
          <xsl:when test="name() = 'column'">
            <td>
              <xsl:attribute name="rowspan">
                <xsl:value-of select="$tree-depth - $recursion +1"/>
              </xsl:attribute>
              <xsl:value-of select="@desc"/>
            </td>
          </xsl:when>
          <xsl:when test="name() = 'group'">
            <td>
              <xsl:attribute name="colspan">
                <xsl:value-of select="count(descendant::column)"/>
              </xsl:attribute>
              <xsl:value-of select="@desc"/>
            </td>
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>
      </tr>
                                                                                                                             
      <!--go to next level-->
    <xsl:variable name="nodes" select="$nodes/*" />
    <xsl:if test="$nodes">
    <xsl:call-template name="header">
      <xsl:with-param name="nodes" select="$nodes" />
      <xsl:with-param name="tree-depth" select="$tree-depth"/>
      <xsl:with-param name="recursion" select="$recursion +1"/>
    </xsl:call-template>
   </xsl:if>
  </xsl:template>
                                                                                                                             
</xsl:stylesheet>






On Thu, 2004-02-05 at 12:54, Rui Alberto L. Gonçalves wrote:
> Hi Manolis,
> thanks for your help! I'm almost there, thanks to you..
> I can't figure out how to calculate the rowspan!!
> 
> My current stylesheet is :
> 
> 
> <?xml version="1.0"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> version="1.1">
>   <xsl:output method="html"/>
>                                                                                                                              
>   <xsl:template match="root">
>     <table border="1">
>       <xsl:call-template name="header">
>         <xsl:with-param name="nodes" select="."/>
>       </xsl:call-template>
>     </table>
>   </xsl:template>
>                                                                                                                              
>                                                                                                                           
>   <xsl:template name="header">
>     <xsl:param name="nodes" select="."/>
>       <tr>
>       <xsl:for-each select="$nodes/*">
>         <xsl:choose>
>           <xsl:when test="name() = 'column'">
>             <td>
>               <xsl:attribute name="rowspan">
>                 <!--
> 
> rowspan is the max($nodes) tree depth I guess....
> 
> 		-->
>                 <xsl:value-of select=" CURRENT TREE DEPTH HERE ???? "/>
>               </xsl:attribute>
>               <xsl:value-of select="@desc"/>
>             </td>
>           </xsl:when>
>                                                                                                                              
>           <!--E um grupo -->
>           <xsl:when test="name() = 'group'">
>             <td>
>               <!--colspan is the number of descendants-->
>               <xsl:attribute name="colspan">
>                 <xsl:value-of select="count(descendant::*)-1"/>
>               </xsl:attribute>
>               <xsl:value-of select="@desc"/>
>             </td>
>           </xsl:when>
>         </xsl:choose>
>       </xsl:for-each>
>       </tr>
> 
>       <xsl:for-each select="$nodes/*">
>         <xsl:if test="count(*) &gt; 0">
>           <xsl:call-template name="header">
>             <xsl:with-param name="nodes" select="."/>
>           </xsl:call-template>
>         </xsl:if>
>       </xsl:for-each>
>   </xsl:template>
>                                                                                                                              </xsl:stylesheet>
> 
> It does the job, but fails computing the rowspan... 
> 
> Any idea??? Thanks again...
> 
> Rui
> 
> 
> 
> On Wed, 2004-02-04 at 20:16, Manolis Mavrikis wrote:
> > Rui,
> > 
> >  I had something siilar long ago. I can't find it right now, I 'll look 
> > in a while instead of re-writing it now. Some clarifications though will 
> > help:
> > 
> > I guess you want to build an HTML table. So: something like:
> > 
> > <table border="1" id="table1">
> >   <tr>
> >     <td rowspan="3">Col 1</td>
> >     <td rowspan="3">Col2 </td>
> >     <td align="center" colspan="4">More cols</td>   
> >     <td rowspan="3">Col 3</td>
> >     <td rowspan="3">Col 4</td>
> >   </tr>
> >   <tr>
> >     <td rowspan="2">Col 1</td>
> >     <td rowspan="2">Col2</td>
> >     <td align="center" colspan="2">depth cols</td>
> >   </tr>
> >   <tr>
> >     <td>Col1 </td>
> >     <td>Col 2</td>
> >   </tr>
> > </table>
> > 
> > 
> > right ?
> > 
> > The other thing I don't get from this structure is are you going to have 
> > data in the columns ? Like 
> > 
> >   <column desc="Col 2">Hello</column> ? 
> > 
> > Basically the idea is that when you find this root structure (btw is this the root of the document, or the root of this structure that represents tables?) you create the <table> html element and then you call a template with select="*"
> > 
> >     <xsl:choose>
> >     <xsl:when test="name()='column'">
> >     <td>
> > 	<xsl:attribute name="rowspan"><xsl:value-of select="count(group)+1"></xsl:attribute>
> > 	<xsl:value-of select="@desc"/>
> >      </td>
> >      </xsl:when><xsl:when test="name()='group'>
> >      <td>
> > 	<xsl:attribute name="colspan"><xsl:value-of select="count(descendant::*)-1"></xsl:attribute>
> > 	<xsl:value-of select="@desc"/>
> >      </td>
> >     </xsl:when></xsl:choose>
> > </xsl:template>
> > 
> > 
> > or something like that (that would basically count the elements inside the group and the descandant group(s) in order to know your colspan. 
> > 
> > Then ypu call another tempalte selecting all groups that would write you the TRs which would again call the previous tempalte fro columns. 
> > 
> > I am sorry I can't be more detailed or if this sounds confusing its out of my mind I haven't tried you. I 'll have a look if I find the stylesheet where I was doing something similar. Meanwhile if this turns out to be helpful please post saying you found it (it will save me time).
> > 
> > Cheers, 
> > 
> > Manolis
> > 
> > Rui Alberto L. Gonçalves wrote:
> > 
> > >Hi all,
> > >
> > >I have the following document:
> > ><root>
> > >  <column desc="Col 1"/>
> > >  <column desc="Col 2"/>
> > >  <group desc="More Cols">
> > >    <column desc="Col 1"/>
> > >    <column desc="Col 2"/>
> > >    <group desc="Depth Col">
> > >      <column desc="Col 1"/>
> > >      <column desc="Col 2"/>
> > >    </group>
> > >  </group>
> > >  <column desc="Col 3"/>
> > >  <column desc="Col 4"/>
> > ></root>
> > >
> > >and I want to build a table with the header respecting
> > >the document structure, that should look like:
> > >
> > >TABLE:
> > >================================================================
> > >        |       |         More Cols            |       |       |
> > > Col 1  | Col 2 | Col 1  | Col 2 | Depth Col   | Col 3 | Col 4 |
> > >        |       |        |       |Col 1 | Col 2|       |       |
> > >===============================================================|
> > >
> > >I'm stucked with this problem ... does anyone have
> > >any sugestion or has done something similar??
> > >Thaks for any help!!
> > >
> > >Rui
> > >
> > >  
> > >
> > 
> > 
> > 
> > 
> >  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
-- 
Rui Alberto L. Gonçalves <rui-l-goncalves@xxxxxxxxxxxxx>
PT Inovação


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


Current Thread