RE: [xsl] RE: Counting preceding elemets of similar node, but in different branch

Subject: RE: [xsl] RE: Counting preceding elemets of similar node, but in different branch
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 23 Jul 2008 14:22:56 +0100
Using position() looks perfectly correct here if your xsl:apply-templates is
written the way you say it is. It wouldn't be correct however if you reach
the QUESTION elements indirectly via intermediate elements. Perhaps you need
to show a more complete stylesheet.

It might be safer however to use <xsl:number level="any"/>.

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

> -----Original Message-----
> From: Karlmarx Rajangam [mailto:karlmarx.rajangam@xxxxxxxxxxxxxxxxxx] 
> Sent: 23 July 2008 14:16
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] RE: Counting preceding elemets of similar 
> node, but in different branch
> 
> I have requirement to display certain id's in a sequential 
> numbering order. My xml is something like [and pls ignore any 
> spell / case mistakes]
> 
> 	<root>
> 		<something>
> 			<assessQ>
> 				<header/>
> 				<Block>
> 					<blockID>xxxx</blockID>
> 					<questions>
> 						<QUESTION ID="1" />
> 						<QUESTION ID="2" />
> 						<QUESTION ID="3"/>
> 					</questions>
> 				</Block>
> 			</assessQ>
> 			<assessQ>
> 				<header/>
> 				<Block>
> 					<blockID>yyy</blockID>
> 					<questions>
> 						<QUESTION ID="1" />
> 						<QUESTION ID="2" />
> 					</questions>
> 				</Block>
> 			</assessQ>
> 		</something>
> 	</root>		
> 	
> And in my xslt (Ver 1), I have something like 
> <xsl:apply-templates 
> select="something/assessQ/Block/questions"/> [I think I 
> cannot change this due to various other elements]
> 	
> And what I am do is something like
> 1= 1
> 2= 2
> 3= 3
> 4= 1
> 5= 2 and so on....
> 
> so, i tried to use position, 
> 
> <xsl:template match="QUESTION">
> 	<xsl:value-of select="position()"/> = <xsl:value-of 
> select="@ID"/><br/>
> </xsl:template>	
> 
> but I released the mistake after seeing the result, 3=3 is 
> being followed by again by 1=1, ... instead of 4=1.
> 
> So, i think i must use preceding-sibling and/or some other 
> logic to calculate how many <QUESTION/> elements are there 
> before it. Pls can you suggest the best way to handle this? 
> Hope there might be some solution?
> 
> Thanks,
> karl

Current Thread