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

G

Gerald Aichholzer

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
 
M

Martin Honnen

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.
 
G

Gerald Aichholzer

Martin said:
Gerald said:
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
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,247
Members
46,844
Latest member
JudyGvh32

Latest Threads

Top