Hi,
I currently have a xml file like this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<config>
<field1 sort="group">field1</field1>
<field2 sort="group">field2</field2>
<field3>field3</field3>
<field4>field4</field4>
</config>
<rows>
<row>
<field1>c</field1>
<field2>g</field2>
<field3>c</field3>
<field4>test2</field4>
</row>
<row>
<field1>a</field1>
<field2>b</field2>
<field3>z</field3>
<field4>test</field4>
</row>
<row>
<field1>a</field1>
<field2>b</field2>
<field3>a</field3>
<field4>test2</field4>
</row>
</rows>
</data>
Now what I want to to is to generate a HTML table which is grouped according to the config-element, eg grouped by
field1,field2 and then
field3 and
field4 ungrouped (as specified in the config).
My current (working) approach is this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="html" indent="yes" version="5.0"/>
<xsl:template match="data">
<html>
<body>
<table border="1">
<thead>
<tr>
<xsl:for-each select="config/*">
<th><xsl:value-of select="."/></th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:call-template name="table-helper">
<xsl:with-param name="config" select="config"/>
</xsl:call-template>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template name="table-helper">
<xsl:param name="config" required="yes"/>
<xsl:param name="column" select="1" as="xs:integer"/>
<xsl:param name="first-round" select="xs:boolean(1)" as="xs:boolean"/>
<xsl:variable name="current-column" select="$config/*[$column]"/>
<xsl:variable name="current-data" select="if($first-round) then rows/row else current-group()"/>
<xsl:choose>
<xsl:when test="$current-column/@sort = 'group'">
<xsl:for-each-group select="$current-data"
group-by="*[local-name() = $current-column/local-name()]">
<xsl:call-template name="table-helper">
<xsl:with-param name="first-round" select="xs:boolean(0)"/>
<xsl:with-param name="column" select="$column + 1"/>
<xsl:with-param name="config" select="$config"/>
</xsl:call-template>
</xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="$current-data">
<tr>
<xsl:for-each select="for $i in $config/* return *[local-name() = $i/local-name()]">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Now while this works I am wondering if I can rewrite that to
apply-templates and if rewriting makes sense at all. (Also: If there are any non-xsl-isms in the template please tell me)
Thx,
Florian