RE: [xsl] position behavior in nested grouping

Subject: RE: [xsl] position behavior in nested grouping
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 15 Jul 2009 09:01:02 +0100
><section-head> should come only in the first group and should not come in
the second group. When I use position() in outer group I am getting the
correct value of the position but when I use position() in nested/inner
group the value is always 1. How to achieve the correct position value in
the nested/inner group.

Bind a variable to the value of position in the outer loop and refer to the
variable in the inner loop.

<xsl:variable name="pos" select="position()"/>

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay  

> -----Original Message-----
> From: Ganesh Babu N [mailto:nbabuganesh@xxxxxxxxx] 
> Sent: 15 July 2009 07:10
> To: XSL
> Subject: [xsl] position behavior in nested grouping
> 
> Hai,
> 
> I am having the input like below:
> 
> <articles>
>       <row>
>          <col>627</col>
>          <col>Abstracts of the Series of Fascia Congress - 
> Part 4</col>
>          <col>270</col>
>       </row>
>       <row>
>          <col>661</col>
>          <col>Abstracts of the Series of Fascia Congress - 
> Part 4</col>
>          <col>270</col>
>       </row>
>       <row>
>          <col>676</col>
>          <col>Abstracts of the Series of Fascia Congress - 
> Part 4</col>
>          <col>271</col>
>       </row>
>       <row>
>          <col>719</col>
>          <col>Abstracts of the Series of Fascia Congress - 
> Part 4</col>
>          <col>272</col>
>       </row>
>       <row>
>          <col>706</col>
>          <col>Prevention &amp; Rehabilitation|Editorial</col>
>          <col>276</col>
>       </row>
>       <row>
>          <col>690</col>
>          <col>Prevention &amp; Rehabilitation|Editorial</col>
>          <col>283</col>
>       </row>
>       <row>
>          <col>721</col>
>          <col>Prevention &amp; Rehabilitation|Learning 
> Methodology</col>
>          <col>291</col>
>       </row>
>    </articles>
> 
> 
> My expected output is as follows:
> 
> 			<sections>
>                                <section-title>Contents</section-title>
>                            <section>
> 				<section-head>Abstracts of the 
> Series of Fascia Congress - Part 4</section-head>
> 				<para>
> 					<page>270</page>
> 				</para>
> 				<para>
> 					<page>271</page>
> 				</para>
> 				<para>
> 					<page>271</page>
> 				</para>
> 				<para>
> 					<page>272</page>
> 				</para>
> 			</section>
> 			<section>
> 				<section-head>Prevention &amp; 
> Rehabilitation</section-head>
> 				
> <section-subhead>Editorial</section-subhead>
> 				<para>
> 					<page>273</page>
> 				</para>
> 				<para>
> 					<page>277</page>
> 				</para>
> 			</section>
> 			<section>
> 				<section-subhead>Learning 
> Methodology</section-subhead>
> 				<para>
> 					<page>284</page>
> 				</para>
> 			</section>
> 		</sections>
> 
> My code is as follows:
> 
> 	<xsl:template match="articles">
> 			<sections>
> 				<section-title>Contents</section-title>
> 				<xsl:for-each-group 
> select="row" group-by="col[2]">
> 					<xsl:variable name="head"
> select="substring-before(current-grouping-key(),'&#x0007C;')"/>
> 					<section>
> 						<xsl:choose>
> 							
> <xsl:when test="contains(current-grouping-key(),'&#x0007C;')">
> 								
> <xsl:for-each-group select="current-group()"
> group-by="substring-after(current-grouping-key(),'&#x0007C;')">
>          						<xsl:if 
> test="position()=1">
>                                                          
> <section-head>
> 								
> 	<xsl:value-of select="$head"/>
> 							</section-head>
>                                                         </xsl:if>
> 							
> <section-subhead>
> 								
> 		<xsl:value-of select="current-grouping-key()"/>
> 								
> 	</section-subhead>
> 								
> 	<xsl:apply-templates select="current-group()"/>
> 								
> </xsl:for-each-group>
> 							</xsl:when>
> 							<xsl:otherwise>
> 								
> <section-head>
> 								
> 	<xsl:value-of select="current-grouping-key()"/>
> 								
> </section-head>
> 								
> <xsl:apply-templates select="current-group()"/>
> 							</xsl:otherwise>
> 						</xsl:choose>
> 					</section>
> 				</xsl:for-each-group>
> 			</sections>
> 	</xsl:template>
> 
> The output of this code is as follows:
> 
>                       <sections>
> 			<section>
> 				<section-head>Abstracts of the 
> Series of Fascia Congress - Part 4</section-head>
> 				<para>
> 					<page>270</page>
> 				</para>
> 				<para>
> 					<page>270</page>
> 				</para>
> 				<para>
> 					<page>271</page>
> 				</para>
> 				<para>
> 					<page>272</page>
> 				</para>
> 			</section>
> 			<section>
> 				<section-head>Prevention &amp; 
> Rehabilitation</section-head>
> 				
> <section-subhead>Editorial</section-subhead>
> 				<para>
> 					<page>276</page>
> 				</para>
> 				<para>
> 					<page>283</page>
> 				</para>
> 			</section>
> 			<section>
> 				<section-head>Prevention &amp; 
> Rehabilitation</section-head>
> 				<section-subhead>Learning 
> Methodology</section-subhead>
> 				<para>
> 					<page>291</page>
> 				</para>
> 			</section>
> 		</sections>
> 
> Problem:
> 
> <section-head> should come only in the first group and should 
> not come in the second group. When I use position() in outer 
> group I am getting the correct value of the position but when 
> I use position() in nested/inner group the value is always 1. 
> How to achieve the correct position value in the nested/inner group.
> 
> Regards,
> Ganesh

Current Thread