I need to transform a div-based XHTML layout into a table based layout using XSLT 1.0. For the basic transformation, I have a stylesheet (below) that creates the table structure just fine.
I cannot figure out how to parse multiple class attributes on the input XHTML to add table-specific attributes to the output. (Yes, I would like these as new table attributes, even though classes are copied across)
My sample XHTML is:
<div class="table align-center">
<div class="tr">
<div class="td"><p>Table Cell 1</p></div>
<div class="td"><p>Table Cell 2</p></div>
</div>
</div>
A basic XSL that builds the table structure OK, is as follows:
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div[contains(@class, 'table')]">
<table>
<xsl:copy-of select="attribute::node()"/>
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="div[contains(@class, 'tr')]">
<tr>
<xsl:copy-of select="attribute::node()"/>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="div[contains(@class, 'td')]">
<td>
<xsl:copy-of select="attribute::node()"/>
<xsl:apply-templates/>
</td>
</xsl:template>
This stylesheet produces:
<table class="table align-center">
<tr class="tr">
<td class="td"><p>Table Cell 1</p></td>
<td class="td"><p>Table Cell 2</p></td>
</tr>
</table>
What I would like to produce is this:
<table class="table align-center" align="center">
<tr class="tr">
<td class="td"><p>Table Cell 1</p></td>
<td class="td"><p>Table Cell 2</p></td>
</tr>
</table>
Is it possible to do this with XSLT 1.0? I’d like the solution to be general enough to add 2 or more classes and parse them to add the required table attributes.
Thank you!
This XSLT 1.0 style-sheet…
…when applied to this document…
…yields…*