Re: [xsl] Inserting Title before Tgroup and Creating Spans

Subject: Re: [xsl] Inserting Title before Tgroup and Creating Spans
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 09 Jan 2009 09:54:37 -0500

At 08:48 AM 1/9/2009, you wrote:
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>.

This problem is essentially similar to the one you posted a few days ago.

Since you want the title to appear before the tgroup, the best place to process the caption is where the tgroup is generated. This is effectively what you are doing when you have (before the tgroup is generated in the template matching 'table'):

<xsl:if test="child::caption"><title><xsl:value-of select="caption"/></title></xsl:if>

which is effectively correct, although it could be neater:

<xsl:for-each select="caption">

You want to use apply-templates in case the caption contains any markup that needs to be processed. Using for-each instead of the if/@test allows you to compress your logic, since the conditional is accomplished at the same time as the change of context to the caption.

This leaves only the problem you go on to note:

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).

... which is happening because the caption is processed again when it is selected by the xsl:apply-templates inside the 'tgroup' you generate.

Suppress it from appearing here simply by matching it with an empty template:

<xsl:template match="caption"/>

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.

This is, as you say, much trickier.

And much easier to accomplish in XSLT 2.0, which you should consider using if you can.

If you can't -- fortunately for you, this is a standard CALS/OASIS table model problem. So your best approach is to locate and borrow code that handles this (or at least gives you a good start). The Docbook XSLT distribution has some. Or for something a bit easier to work with, check out the XSLT distribution we made for the Extreme series of conferences, which includes the Docbook code in a discrete module.

Find it at

Good luck --

Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

Current Thread