Reputation: 428
The XML matrix with csv rows
<matrix ROWS="3" COLS="4">
<row>"1,2,3,4"</row>
<row>"5,6,7,8"</row>
<row>"9,10,11,12"</row>
</matrix>
is to be transformed with XSLT 1.0 without extensions into a HTML table
<table>
<tr><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr><td>5</td><td>6</td><td>7</td><td>8</td></tr>
<tr><td>9</td><td>10</td><td>11</td><td>12</td></tr>
</table>
The number of rows and columns (ROWS, COLS) are variable.
I appreciate any help.
Upvotes: 0
Views: 277
Reputation: 116993
How about:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/matrix">
<table>
<xsl:for-each select="row">
<tr>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring(., 2, string-length(.) - 2)"/>
</xsl:call-template>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="','"/>
<xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:if test="$token">
<td>
<xsl:value-of select="$token"/>
</td>
</xsl:if>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2