RE: [xsl] Multiple for-each-group in a single template

Subject: RE: [xsl] Multiple for-each-group in a single template
From: "V.Ramkumar" <v.ramkumar@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 20 Nov 2008 17:16:26 +0530
Dear David,

As you told, Input and required out are given below,

Input:
<html>
<body>
	<p class="heading-1">Heading 1</p>
		<p>para 1</p>
		<p class="box">para 2</p>
		<p class="box">para 3</p>
		<p>para 4</p>
	<p class="heading-2">Heading 2</p>
		<p>para 1</p>
		<p class="exercise">para 2</p>
		<p class="exercise">para 3</p>
		<p>para 4</p>
</body>
</html>

Exp.Output:

<chapter>
<body>
	<section title="Heading-1">
		<p>para 1</p>
		<box>
			<p>para 2</p>
			<p>para 3</p>
		</box>
			<p>para 4</p>
	</section>
	<section title="Heading-2">
		<p>para 1</p>
		<excecise>
			<p>para 2</p>
			<p>para 3</p>
		</excecise>
		<p>para 4</p>
	</section>
</body>
</chapter>

My XSLT:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="xml" indent="no" encoding="iso-8859-1"
omit-xml-declaration="yes" />
	<xsl:template match="html">
		<chapter>
			<xsl:apply-templates/>
		</chapter>
	</xsl:template>

	<xsl:template match="*">
		<xsl:copy>
			<xsl:copy-of select="@*"/>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="body">
	<body>
		<xsl:for-each-group select="*"
group-adjacent="string(self::p/@class)">
			<xsl:choose>
				<xsl:when
test="self::p/@class[contains(.,'box')]">
					<box type="box"
page="{preceding-sibling::p[@class='pageNumber'][1]}">
						<xsl:apply-templates
select="current-group()"/>
					</box>
				</xsl:when>
				<xsl:when
test="self::p/@class[contains(.,'exercise')]">
					<exercise>
						<xsl:apply-templates
select="current-group()"/>
					</exercise>
				</xsl:when>
				<xsl:when
test="self::p/@class[contains(.,'marginText')]">
					<margintext type="quotation">
						<xsl:apply-templates
select="current-group()"/>
					</margintext>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates
select="current-group()"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each-group>
		<xsl:for-each-group select="*"
group-starting-with="p[@class='heading-1']">
			<xsl:choose>
				<xsl:when
test="self::p/@class[contains(.,'heading-1')]">
					<section1>
						<xsl:apply-templates
select="current-group()"/>
					</section1>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates
select="current-group()"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each-group>
</body>
</xsl:template>
Regards,
Ramkumar.

Current Thread