I have a problem that I have solved using XSLT 2.0, but now I need to be able to do the same thing in XSLT 1.0 (because of a constraint to use an XSLT 1.0 compatible processor).
In fact, I have the need for different kinds of XHTML and XML, and possibly slightly different scenarios, but I'll give an example in XHTML for simplicity to find the general solution to this:
Say I have an HTML table like this:
<table frame="void">
<col width="50%" />
<col width="50%" />
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>text text <b>text</b> text <i>text</i> text</td>
<td>text text <b>text</b> text <i>text</i> text<img src="mypic.png" alt="mypic"
/>text <b>text</b> text</td>
</tr>
<tr>
<td>
<table frame="void">
<col width="50%" />
<col width="50%" />
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</td>
<td><img src="mypic.png" alt="mypic" /></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
And what I want now is to wrap all the text and inline elements in the table cells with <p>
tags, to get this:
<table frame="void">
<col width="50%" />
<col width="50%" />
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p>text text <b>text</b> text <i>text</i> text</p>
</td>
<td><p>text text <b>text</b> text <i>text</i> text</p><img src="mypic.png"
alt="mypic" /><p>text <b>text</b> text</p></td>
</tr>
<tr>
<td>
<table frame="void">
<col width="50%" />
<col width="50%" />
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</td>
<td><img src="mypic.png" alt="mypic" /></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
Note that a couple other cells have an image and a nested table. If there is only an image or a table in those cells (and no text or inline elements), they should not be wrapped in p tags.
Also note that one of the images have surrounding text and inline elements. In such a case the text and inline before the image should be wrapped in a p tag before the image (or table or whatever non-inline element it might be), and the text/inline after the image should be wrapped in another p tag. (In this use case img is considered a non-inline element btw).
When doing this in XSLT 2.0 I have used this template to handle this, calling it from the template for table cells instead of just applying child templates:
<xsl:template name="wrapInPara">
<xsl:apply-templates select="@class"></xsl:apply-templates>
<xsl:for-each-group select="node()[not(self::text() and normalize-space(.) = '')]"
group-adjacent="boolean(self::text() | self::e:b | self::e:i | self::e:em | self::e:strong | self::e:a | self::e:u | self::e:span)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<p>
<xsl:apply-templates select="current-group()"/>
</p>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
And calling it something like this:
<xsl:template match="td">
<xsl:copy>
<xsl:call-template name="wrapInPara"/>
</xsl:copy>
</xsl:template>
(As you can see there are more than the <b>
and <i>
tags that need to be considered, and it could be other tags than nested tables or images that should be excluded from the wrapping, so I'm hoping for an answer that can be modified for similar use cases if possible.
I have been trying to figure out how to do something like this in XSLT 1.0 looking at the Muenchian grouping method, which similar problems seem to suggest, but I can't get it to work.
Any help greatly appreciated!