XSLT compute position of parent

Discussion in 'XML' started by Stéphane Mottelet, May 20, 2008.

  1. 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.
     
    Stéphane Mottelet, May 20, 2008
    #1
    1. Advertising

  2. Stéphane Mottelet wrote:
    > 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.




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 20, 2008
    #2
    1. Advertising

  3. Martin Honnen a écrit :
    > Stéphane Mottelet wrote:
    >> 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.
    >
    >
    >
    >


    thanks !

    S.
     
    Stéphane Mottelet, May 20, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luqman
    Replies:
    1
    Views:
    662
    Luqman
    Feb 7, 2006
  2. datamodel
    Replies:
    6
    Views:
    692
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=
    Jan 16, 2007
  3. James Wong
    Replies:
    4
    Views:
    514
    James Wong
    Jul 14, 2004
  4. PerlFAQ Server
    Replies:
    0
    Views:
    275
    PerlFAQ Server
    Feb 2, 2011
  5. PD
    Replies:
    6
    Views:
    730
Loading...

Share This Page