xsl-list
[Top] [All Lists]

Re: [xsl] Re: HTML table colspec and spanspec problem

2010-01-23 09:37:54
Siddhi,
This xsl will get close. Needs more work on picking the right alignment based 
on count.
XSL NEW TEMPLATE ADDED AT THE TOP
Terry

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:xs="http://www.w3.org/2001/XMLSchema"; exclude-result-prefixes="xs" 
version="2.0">
 <xsl:template match="table">
  <xsl:result-document href="output.xml">
   <table>
    <tgroup>
     <xsl:for-each-group select="tbody/tr/td" group-by=".">
      <!-- FIRST GROUP BY COLUMN NAME -->
      <xsl:choose>
       <!-- PICK WHICH TYPE OF SPEC -->
       <xsl:when test="contains(. , 'and')">
        <spanspec>
         <xsl:attribute name="name">
          <xsl:value-of select="substring-before(current-group()[1] , ' and 
')"/>
          <xsl:text>to</xsl:text>
          <xsl:value-of select="substring-after(current-group()[1] , ' and ')"/>
         </xsl:attribute>
         <xsl:attribute name="width"/>
         <xsl:attribute name="align" select="current-group()[1]/@align"/>
         <xsl:attribute name="namestart"
          select="substring-before(current-group()[1] , ' and ')"/>
         <xsl:attribute name="nameend" 
select="substring-after(current-group()[1]
          , ' and ')"/>
        </spanspec>
       </xsl:when>
       <xsl:otherwise>
        <colspec>
         <xsl:attribute name="name">
          <xsl:value-of select="current-grouping-key()"/>
         </xsl:attribute>
         <xsl:attribute name="width"/>
         <xsl:attribute name="align" select="current-group()[1]/@align"/>
        </colspec>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:for-each-group>
     <xsl:apply-templates/>
    </tgroup>
   </table>
  </xsl:result-document>
 </xsl:template>
 <xsl:template match="tbody">
  <xsl:apply-templates/>
 </xsl:template>
 <xsl:template match="tr" mode="abc">
  <xsl:if test="preceding::tr[1][child::td[(_at_)class='tch']]">
   <row>
    <xsl:apply-templates/>
   </row>
   <xsl:apply-templates select="following::tr[1][child::td[(_at_)class='tch']]" 
mode="abc"/>
  </xsl:if>
 </xsl:template>
 <xsl:template match="tr" mode="def">
  <xsl:if test="preceding::tr[1][child::td[(_at_)class='tbtm']]">
   <row>
    <xsl:apply-templates/>
   </row>
   <xsl:apply-templates 
select="following::tr[1][child::td[(_at_)class='tbtm']]" mode="def"/>
  </xsl:if>
 </xsl:template>
 <xsl:template match="tr" mode="xyz">
  <xsl:if test="preceding::tr[1][child::td[(_at_)class='tb']]">
   <row>
    <xsl:apply-templates/>
   </row>
   <xsl:apply-templates select="following::tr[1][child::td[(_at_)class='tb']]" 
mode="xyz"/>
  </xsl:if>
 </xsl:template>
 <xsl:template match="tr">
  <xsl:if 
test="child::td[(_at_)class='tch'][not(preceding::tr[1][child::td[(_at_)class='tch']])]">
   <thead>
    <row>
     <xsl:apply-templates/>
    </row>
    <xsl:if test="following::*[1][self::tr[child::td[(_at_)class='tch']]]">
     <xsl:apply-templates 
select="following::tr[1][child::td[(_at_)class='tch']]" mode="abc"/>
    </xsl:if>
   </thead>
  </xsl:if>
  <xsl:if 
test="child::td[(_at_)class='tbtm'][not(preceding::tr[1][child::td[(_at_)class='tbtm']])]">
   <tfoot>
    <row>
     <xsl:apply-templates/>
    </row>
    <xsl:if test="following::*[1][self::tr[child::td[(_at_)class='tbtm']]]">
     <xsl:apply-templates 
select="following::tr[1][child::td[(_at_)class='tbtm']]" mode="def"/>
    </xsl:if>
   </tfoot>
  </xsl:if>
  <xsl:if 
test="child::td[(_at_)class='tb'][not(preceding::tr[1][child::td[(_at_)class='tb']])]">
   <tbody>
    <row>
     <xsl:apply-templates/>
    </row>
    <xsl:if test="following::*[1][self::tr[child::td[(_at_)class='tb']]]">
     <xsl:apply-templates 
select="following::tr[1][child::td[(_at_)class='tb']]" mode="xyz"/>
    </xsl:if>
   </tbody>
  </xsl:if>
 </xsl:template>

 <xsl:template match="td">
  <entry>
   <xsl:attribute name="class">
    <xsl:value-of select="@class"/>
   </xsl:attribute>
   <xsl:if test="@colspan">
    <xsl:attribute name="colspan">
     <xsl:value-of select="@colspan"/>
    </xsl:attribute>
   </xsl:if>
   <xsl:attribute name="name">
    <xsl:text>col</xsl:text>
    <xsl:if test="not(@colspan)">
     <xsl:if test="not(preceding-sibling::td[(_at_)colspan])">
      <xsl:value-of select="count(preceding-sibling::td)+1"/>
     </xsl:if>
     <xsl:if test="preceding-sibling::td[(_at_)colspan]">
      <xsl:variable name="colspan_values"
       select="sum(preceding-sibling::td[(_at_)colspan]/@colspan)"/>
      <xsl:variable name="td_number" select="count(preceding-sibling::td)"/>
      <xsl:variable name="colspan_td_number"
       select="count(preceding-sibling::td[(_at_)colspan])"/>
      <xsl:value-of select="($td_number+$colspan_values+1) - 
$colspan_td_number"/>
     </xsl:if>
    </xsl:if>
    <xsl:if test="@colspan">
     <xsl:if test="not(preceding-sibling::td[(_at_)colspan])">
      <xsl:value-of select="count(preceding-sibling::td)+1"/>
      <xsl:text>to</xsl:text>
      <xsl:variable name="td_number" select="count(preceding-sibling::td)"/>
      <xsl:variable name="colspan" select="@colspan"/>
      <xsl:value-of select="count(preceding-sibling::td)+(_at_)colspan"/>
     </xsl:if>
     <xsl:if test="preceding-sibling::td[(_at_)colspan]">
      <xsl:variable name="colspan_values"
       select="sum(preceding-sibling::td[(_at_)colspan]/@colspan)"/>
      <xsl:variable name="td_number" select="count(preceding-sibling::td)"/>
      <xsl:variable name="colspan_number"
       select="count(preceding-sibling::td[(_at_)colspan])"/>
      <xsl:variable name="starting_value">
       <xsl:value-of select="($td_number+$colspan_values+1) - $colspan_number"/>
      </xsl:variable>
      <xsl:value-of select="$starting_value"/>
      <xsl:text>to</xsl:text>
      <xsl:value-of select="($starting_value+(_at_)colspan) - 1"/>
     </xsl:if>
    </xsl:if>
   </xsl:attribute>
   <xsl:apply-templates/>
  </entry>
 </xsl:template>

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



--~--


      

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--