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: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Wed, 23 Jul 2008 18:52:15 +0530
It seems, following would work

<xsl:template match="QUESTION">
  <xsl:value-of select="count(preceding::QUESTION) + 1"/> = <xsl:value-of
select="@ID"/><br/>
</xsl:template>

On 7/23/08, Karlmarx Rajangam <karlmarx.rajangam@xxxxxxxxxxxxxxxxxx> wrote:
> 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


-- 
Regards,
Mukul Gandhi

Current Thread