XSLT: select n-th element (how to use position?)

Discussion in 'XML' started by Gerald Aichholzer, May 29, 2005.

  1. Hello NG,

    I have an XML document looking similiar to:

    <document>
    <section>
    <group>
    <symbol>...</symbol> <!-- n=1 -->
    <symbol>...</symbol> <!-- n=2 -->
    ...
    <symbol>...</symbol> <!-- n=x -->
    </group>
    <symbol>...</symbol> <!-- n=x+1 -->
    <symbol>...</symbol> <!-- n=x+2 -->
    ...
    <group>
    </group>
    ...
    </section>
    <section>
    </section>
    ...
    </document>


    I need to select the n-th symbol (and all subnodes),
    where n is the symbol number in the whole document.

    Is this possible? If it is how will I be able to
    achieve this?

    AFAIK position() starts counting with 1 for each
    parent element - isn't it?

    thanx in advance,
    Gerald
     
    Gerald Aichholzer, May 29, 2005
    #1
    1. Advertising

  2. Gerald Aichholzer wrote:


    > I have an XML document looking similiar to:
    >
    > <document>
    > <section>
    > <group>
    > <symbol>...</symbol> <!-- n=1 -->
    > <symbol>...</symbol> <!-- n=2 -->
    > ...
    > <symbol>...</symbol> <!-- n=x -->
    > </group>
    > <symbol>...</symbol> <!-- n=x+1 -->
    > <symbol>...</symbol> <!-- n=x+2 -->
    > ...
    > <group>
    > </group>
    > ...
    > </section>
    > <section>
    > </section>
    > ...
    > </document>
    >
    >
    > I need to select the n-th symbol (and all subnodes),
    > where n is the symbol number in the whole document.


    You can always do
    (//symbol)[last()]
    to access the last of all <symbol> elements at all levels.
    Perhaps
    (/document/section//symbol)[last()]
    is all you need with that XML.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 29, 2005
    #2
    1. Advertising

  3. Martin Honnen wrote:
    >
    > Gerald Aichholzer wrote:
    >
    >> I have an XML document looking similiar to:
    >>
    >> <document>
    >> <section>
    >> <group>
    >> <symbol>...</symbol> <!-- n=1 -->
    >> <symbol>...</symbol> <!-- n=2 -->
    >> ...
    >> <symbol>...</symbol> <!-- n=x -->
    >> </group>
    >> <symbol>...</symbol> <!-- n=x+1 -->
    >> <symbol>...</symbol> <!-- n=x+2 -->
    >> ...
    >> <group>
    >> </group>
    >> ...
    >> </section>
    >> <section>
    >> </section>
    >> ...
    >> </document>
    >>
    >>
    >> I need to select the n-th symbol (and all subnodes),
    >> where n is the symbol number in the whole document.

    >
    >
    > You can always do
    > (//symbol)[last()]
    > to access the last of all <symbol> elements at all levels.
    > Perhaps
    > (/document/section//symbol)[last()]
    > is all you need with that XML.
    >


    Thank you for your information. n is specifid in a
    parameter. Now I'm using the following wich works:

    (/document//symbol)[position()=$symbolpos]


    Gerald
     
    Gerald Aichholzer, May 29, 2005
    #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. Igor
    Replies:
    1
    Views:
    5,027
    Dimitre Novatchev
    Jul 20, 2003
  2. Replies:
    1
    Views:
    521
    Richard Tobin
    Dec 9, 2005
  3. HANM
    Replies:
    2
    Views:
    762
    Joseph Kesselman
    Jan 29, 2008
  4. Konrad Hammerer
    Replies:
    14
    Views:
    628
    Jonathan N. Little
    Feb 7, 2008
  5. palmiere
    Replies:
    1
    Views:
    464
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page