[xsl] Inserting Title before Tgroup and Creating Spans

Subject: [xsl] Inserting Title before Tgroup and Creating Spans
From: Charles Flanders <cflanders@xxxxxxxxxx>
Date: Fri, 09 Jan 2009 08:48:18 -0500
I need assistance with two problems.

1. How can I transform <caption> to <title> and insert the <title> before <tgroup> in this HTML table to XML Exchange Table conversion. Caption does not always exist. It is optional in the current model. I originally created a template for "caption" but my output had <caption> as a child of <tgroup>. <caption> needs to be a preceding sibling to <tgroup>. My current XSLT (an IF for <caption>) gives me nearly the result I need with the exception of the title text being output as part of <tgroup>. I'm sure there is to create a template for <caption> and insert the resulting <title> as a child of table but a preceding sibling of <tgroup>. (standard table structure).

2. My second problem is a little more complex. I need to transform the colspan=(0-9) attribute to a "namest" and "nameend" attributes for the column spans. I have it figured out if there happens to only be a single span in a row. But if there are additional spans after the first span, I'm having trouble figuring out how to calculate the begin and end columns for any second and succeeding spans. Any help is much appreciated. My XSLT follows first, then my current output, then the desired output.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:template match="table">
<table>
<xsl:if test="./@outputclass">
<xsl:attribute name="frame">
<xsl:if test="@outputclass='noBorders'">none</xsl:if>
<xsl:if test="@outputclass='fullBorders'">all</xsl:if>
<xsl:if test="@outputclass='default'">topbot</xsl:if>
</xsl:attribute>
</xsl:if>
<xsl:if test="./@id">
<xsl:attribute name="id"><xsl:value-of select="@id"/>
</xsl:attribute></xsl:if>
<xsl:if test="./@rowheader">
<xsl:attribute name="rowheader">
<xsl:value-of select="@rowheader"/>
</xsl:attribute></xsl:if>
<xsl:if test="./@framewidth">
<xsl:attribute name="pgwide">
<xsl:if test="@framewidth='wide'">1</xsl:if>
</xsl:attribute>
</xsl:if>
<xsl:if test="./@page-orientation">
<xsl:attribute name="otherprops"><xsl:value-of select="@page-orientation"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="child::caption"><title><xsl:value-of select="caption"/></title></xsl:if>
<tgroup cols="{count(./descendant::tr[1]/child::*)}">
<xsl:if test="not(col)"><xsl:for-each select="/descendant::tr[1]/child::*">
<colspec width="*"/>
</xsl:for-each>
</xsl:if>
<xsl:apply-templates/>
</tgroup></table>
</xsl:template> <xsl:template match="tbody">
<tbody><xsl:apply-templates/></tbody>
</xsl:template>
<xsl:template match="tr">
<row><xsl:apply-templates/></row>
</xsl:template>
<xsl:template match="td">
<entry>
<xsl:if test="./@align">
<xsl:attribute name="align">
<xsl:value-of select="./@align"/></xsl:attribute>
</xsl:if>
<xsl:if test="./@valign">
<xsl:attribute name="valign">
<xsl:value-of select="./@valign"/></xsl:attribute>
</xsl:if>
<xsl:if test="./@width">
<xsl:attribute name="width">
<xsl:value-of select="./@width"/></xsl:attribute>
</xsl:if>
<xsl:if test="./@class">
<xsl:attribute name="class">
<xsl:value-of select="./@class"/></xsl:attribute>
</xsl:if>
<xsl:if test="./@rowspan">
<xsl:attribute name="morerows">
<xsl:value-of select="./@rowspan"/></xsl:attribute>
</xsl:if>
<xsl:if test="./@colspan"><xsl:variable name="colcount"><xsl:value-of select="@colspan"/></xsl:variable>
<xsl:variable name="colstart" select="count(preceding-sibling::td)"/>
<xsl:attribute name="namest"><xsl:text>col</xsl:text><xsl:value-of select="$colstart + 1"/></xsl:attribute>
<xsl:attribute name="nameend"><xsl:text>col</xsl:text><xsl:value-of select="$colstart + $colcount"/></xsl:attribute>
</xsl:if>
<xsl:apply-templates/></entry>
</xsl:template>
<xsl:template match="th">
<entry>
<xsl:if test="./@align">
<xsl:attribute name="align">
<xsl:value-of select="./@align"/></xsl:attribute>
</xsl:if>
<xsl:if test="./@valign">
<xsl:attribute name="valign">
<xsl:value-of select="./@valign"/></xsl:attribute>
</xsl:if>
<xsl:if test="./@width">
<xsl:attribute name="width">
<xsl:value-of select="./@width"/></xsl:attribute>
</xsl:if>
<xsl:if test="./@class">
<xsl:attribute name="class">
<xsl:value-of select="./@class"/></xsl:attribute>
</xsl:if>
<xsl:if test="./@rowspan">
<xsl:attribute name="morerows">
<xsl:value-of select="./@rowspan"/></xsl:attribute>
</xsl:if>
<xsl:if test="./@colspan"><xsl:variable name="colcount"><xsl:value-of select="@colspan"/></xsl:variable>
<xsl:variable name="colstart" select="count(preceding-sibling::th)"/>
<xsl:attribute name="namest"><xsl:text>col</xsl:text><xsl:value-of select="$colstart + 1"/></xsl:attribute>
<xsl:attribute name="nameend"><xsl:text>col</xsl:text><xsl:value-of select="$colstart + $colcount"/></xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</entry>
</xsl:template>
<xsl:template match="col[@width]">
<colspec width="{@width}"/>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>



*Input document:*


<?xml version="1.0" encoding="UTF-8"?>
<table id="c-ampcondamplifierconditioning-utbl1" cellspacing="0pt" width="80%">
<caption>TITLE</caption>
<col width="0.9184in"/>
<col width="0.9184in"/>
<col width="0.9184in"/>
<col width="0.9184in"/>
<col width="0.9184in"/>
<col width="0.91841in"/>
<thead>
<tr>
<th class="- topic/th " cellspacing="0pt"/>
<th colspan="2" class="- topic/th " cellspacing="0pt">
<p class="- topic/p ">
<b class="+ topic/ph hi-d/b ">Shelf LEDs</b>
</p>
</th>
<th colspan="3" class="- topic/th " cellspacing="0pt">
<p class="- topic/p ">
<b class="+ topic/ph hi-d/b ">System LEDs</b>
</p>
</th>
</tr>
<tr>
<th class="- topic/th " cellspacing="0pt">
<p class="- topic/p ">
<b class="+ topic/ph hi-d/b ">Location</b>
</p>
</th>
<th class="- topic/th " cellspacing="0pt">
<p class="- topic/p ">
<b class="+ topic/ph hi-d/b ">Trouble</b>
</p>
</th>
<th class="- topic/th " cellspacing="0pt">
<p class="- topic/p ">
<b class="+ topic/ph hi-d/b ">Power</b>
</p>
</th>
<th class="- topic/th " cellspacing="0pt">
<p class="- topic/p ">
<b class="+ topic/ph hi-d/b ">Critical</b>
</p>
</th>
<th class="- topic/th " cellspacing="0pt">
<p class="- topic/p ">
<b class="+ topic/ph hi-d/b ">Major</b>
</p>
</th>
<th class="- topic/th " cellspacing="0pt">
<p class="- topic/p ">
<b class="+ topic/ph hi-d/b ">Minor</b>
</p>
</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top" class="- topic/td " cellspacing="0pt">
<p class="- topic/p ">MSI</p>
</td>
<td align="left" valign="top" class="- topic/td " cellspacing="0pt">
<p class="- topic/p ">ON</p>
</td>
<td align="left" valign="top" class="- topic/td " cellspacing="0pt">
<p class="- topic/p ">ON</p>
</td>
<td align="left" valign="top" class="- topic/td " cellspacing="0pt">
<p class="- topic/p ">OFF</p>
</td>
<td align="left" valign="top" class="- topic/td " cellspacing="0pt">
<p class="- topic/p ">OFF</p>
</td>
<td align="left" valign="top" class="- topic/td " cellspacing="0pt">
<p class="- topic/p ">ON</p>
</td>
</tr>
</tbody>
</table>



*Current output:*


<table id="c-ampcondamplifierconditioning-utbl1"><title>TITLE</title><tgroup cols="3">
TITLE
<colspec width="0.9184in"/>
<colspec width="0.9184in"/>
<colspec width="0.9184in"/>
<colspec width="0.9184in"/>
<colspec width="0.9184in"/>
<colspec width="0.91841in"/>
<row>
<entry class="- topic/th "/>
<entry class="- topic/th " namest="col2" nameend="col3">Shelf LEDs</entry>
<entry class="- topic/th " namest="col3" nameend="col5">System LEDs</entry>
</row>
<row>
<entry class="- topic/th ">Location</entry>
<entry class="- topic/th ">Trouble/entry>
<entry class="- topic/th ">Power</entry>
<entry class="- topic/th ">Critical</entry>
<entry class="- topic/th ">Major</entry>
<entry class="- topic/th ">Minor</entry>
</row>
<tbody>
<row>
<entry align="left" valign="top" class="- topic/td ">MSI</entry>
<entry align="left" valign="top" class="- topic/td ">ON</entry>
<entry align="left" valign="top" class="- topic/td ">ON</entry>
<entry align="left" valign="top" class="- topic/td ">OFF</entry>
<entry align="left" valign="top" class="- topic/td ">OFF</entry>
<entry align="left" valign="top" class="- topic/td ">ON</entry>
</row>
</tbody>
</tgroup></table>


*Desired Output:*

<table id="c-ampcondamplifierconditioning-utbl1"><title>TITLE</title>
<tgroup cols="3">
<colspec width="0.9184in"/>
<colspec width="0.9184in"/>
<colspec width="0.9184in"/>
<colspec width="0.9184in"/>
<colspec width="0.9184in"/>
<colspec width="0.91841in"/>
<row>
<entry class="- topic/th "/>
<entry class="- topic/th " namest="col2" nameend="col3">Shelf LEDs</entry>
<entry class="- topic/th " namest="col4" nameend="col7">System LEDs</entry>
</row>
<row>
<entry class="- topic/th ">Location</entry>
<entry class="- topic/th ">Trouble/entry>
<entry class="- topic/th ">Power</entry>
<entry class="- topic/th ">Critical</entry>
<entry class="- topic/th ">Major</entry>
<entry class="- topic/th ">Minor</entry>
</row>
<tbody>
<row>
<entry align="left" valign="top" class="- topic/td ">MSI</entry>
<entry align="left" valign="top" class="- topic/td ">ON</entry>
<entry align="left" valign="top" class="- topic/td ">ON</entry>
<entry align="left" valign="top" class="- topic/td ">OFF</entry>
<entry align="left" valign="top" class="- topic/td ">OFF</entry>
<entry align="left" valign="top" class="- topic/td ">ON</entry>
</row>
</tbody>
</tgroup></table>
--
*Charles Flanders*

Current Thread