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 18:33:01 +0530
Dear David.

Thanks. It's working fine. I understand the logic.

But Section closed before any section it should close before the same-level.

Xsl:

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

<xsl:output method="xml" indent="yes" 
	    omit-xml-declaration="yes" />

<xsl:strip-space elements="*"/>

<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-starting-with="p[contains(@class,'heading-')]">
			<xsl:choose>
				<xsl:when
test="self::p/@class[contains(.,'heading-1')]">
					<section1>
						<xsl:call-template name="g">
							<xsl:with-param
name="e" select="current-group()"/>
						</xsl:call-template>
					</section1>
				</xsl:when>
				<xsl:when
test="self::p/@class[contains(.,'heading-2')]">
					<section2>
						<xsl:call-template name="g">
							<xsl:with-param
name="e" select="current-group()"/>
						</xsl:call-template>
					</section2>
				</xsl:when>
				<xsl:when
test="self::p/@class[contains(.,'heading-3')]">
					<section3>
						<xsl:call-template name="g">
							<xsl:with-param
name="e" select="current-group()"/>
						</xsl:call-template>
					</section3>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="g">
						<xsl:with-param name="e"
select="current-group()"/>
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each-group>
	</body>
	</xsl:template>

	<xsl:template name="g">
		<xsl:param name="e"/>
		<xsl:for-each-group select="$e"
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:template>

Regards,
Ramkumar

Current Thread