Re: [xsl] xsl:for-each-group and identity transform

Subject: Re: [xsl] xsl:for-each-group and identity transform
From: Jesper Tverskov <jesper.tverskov@xxxxxxxxx>
Date: Fri, 23 Jan 2009 13:40:56 +0100
To make me more clear. I have made an "unflatten.xsl" using Michael
Kay's code in 4th edition of "XSLT 2.0 and XPath 2.0", page 340,  as
point of departure. I have modified it to get real XHTML input
working.

I think my stylesheet works, but I run into the good old problem of
attributes being copied out of the DTD. So I was looking for a way to
get additional templates in place to delete those attributes again as
I use to do when making an identity transform. But I have only managed
to do so in a second transformation.

So here is my question: Is it possible to get both the
xsl:for-each-group and a modified identity transform working in one
transformation?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0" xmlns="http://www.w3.org/1999/xhtml";
xpath-default-namespace="http://www.w3.org/1999/xhtml";>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="html">
<xsl:copy>
<xsl:attribute name="xml:lang">en</xsl:attribute>
<xsl:apply-templates select="head|body"/>
</xsl:copy>
</xsl:template>
<xsl:template match="head">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="body">
<xsl:copy>
<xsl:for-each-group
select="element()|comment()|processing-instruction()"
group-starting-with="h1">
<xsl:apply-templates select="." mode="group"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="h1" mode="group">
<h>
<xsl:copy-of select="descendant::node()"/>
</h>
<xsl:for-each-group select="current-group() except ." group-starting-with="h2">
<xsl:apply-templates select="." mode="group"/>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="h2|h3|h4|h5|h6" mode="group">
<xsl:variable name="this" select="name()"/>
<xsl:variable name="next" select="translate($this, '23456', '34567')"/>
<section>
<h>
<xsl:copy-of select="descendant::node()"/>
</h>
<xsl:for-each-group select="current-group() except ."
group-starting-with="*[name() = $next]">
<xsl:apply-templates select="." mode="group"/>
</xsl:for-each-group>
</section>
</xsl:template>
<xsl:template match="element()|comment()|processing-instruction()" mode="group">
<xsl:copy-of select="current-group()"/>
</xsl:template>
</xsl:stylesheet>

Cheers,
Jesper

Current Thread