XSLT compute position of parent

S

Stéphane Mottelet

Hi, I have the following problem

<stuff>
<e1>
...
</e1>
<e1>
<e2/>
</e1>
</stuff>

When I am in the context of node <e2/>
how can I compute the position of its
parent <e1/> within <stuff> (position=2 here) ?

Thanks for help.

S.
 
M

Martin Honnen

Stéphane Mottelet said:
Hi, I have the following problem

<stuff>
<e1>
...
</e1>
<e1>
<e2/>
</e1>
</stuff>

When I am in the context of node <e2/>
how can I compute the position of its
parent <e1/> within <stuff> (position=2 here) ?

position depends on the current node list and that can be different
depending on select expressions. For instance in your sample above if
you have
<xsl:template match="stuff">
<xsl:apply-templates/>
</xsl:template>
then the second 'e1' element has position 4 as there is white space
between the 'stuff' element and the 'e1' elements.

So it is not clear what you understand the position to be, if you want
to count the siblings of the same node type and name then use e.g.
<xsl:template match="e2">
<xsl:value-of select="count(.. | ../preceding-sibling::e1)"/>
</xsl:template>

xsl:number can also be helpful, depending on your needs.
 
S

Stéphane Mottelet

Martin Honnen a écrit :
position depends on the current node list and that can be different
depending on select expressions. For instance in your sample above if
you have
<xsl:template match="stuff">
<xsl:apply-templates/>
</xsl:template>
then the second 'e1' element has position 4 as there is white space
between the 'stuff' element and the 'e1' elements.

So it is not clear what you understand the position to be, if you want
to count the siblings of the same node type and name then use e.g.
<xsl:template match="e2">
<xsl:value-of select="count(.. | ../preceding-sibling::e1)"/>
</xsl:template>

xsl:number can also be helpful, depending on your needs.

thanks !

S.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top