position() question

F

Felix Natter

hi,

I have a structure like:

<topic name="xxx">
<subtopic name="subxxx">
<subsubtopic name="subsubxxx">
</subsubtopic>
</subtopic>
</topic>
and so on.

in the <topic> template I do this:

<tr>
<!-- alternating background-colors: tr.one/tr.two -->
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="position() mod 2 = 0">
<xsl:text>one</xsl:text>
</xsl:when>
<xsl:eek:therwise>
<xsl:text>two</xsl:text>
</xsl:eek:therwise>
</xsl:choose>
</xsl:attribute>


to set alternating background-colors (which works fine).
But in a template for 'subsubtopic':

<xsl:template match="topic/subtopic/subsubtopic" mode="toc">

I want to find out the position() of the ancestor <topic> element so that
the <tr>'s generated by this template use the same background-color as the
ancestor <topic> template.

this gives an xpath-error:
<xsl:if test="parent::subtopic/parent::topic/position() mod 2 = 0">

So I tried this (this should return an empty node-set if the position of
the <topic> is odd)
<xsl:if test="parent::subtopic/parent::topic[position() mod 2 = 0]">

but it doesn't work. I also tried this:
<xsl:if test="count(parent::subtopic/parent::topic[position() mod 2 = 0])!=0">

but it doesn't work either. can you explain why?
(I could pass a parameter to the subtopic and subsubtopic templates
which indicates background-color but it seems cleaner this way)

thanks,
 
D

Dimitre Novatchev

But in a template for 'subsubtopic':
<xsl:template match="topic/subtopic/subsubtopic" mode="toc">

I want to find out the position() of the ancestor <topic> element so that
the <tr>'s generated by this template use the same background-color as the
ancestor <topic> template.

This is wrong terminology. position() is defined only for the position of
the current node in the current node-list.

position() also depends on how the template was instantiated (the "select"
attribute of xsl:apply-templates) and can vary for different ways to use
xsl:apply-templates.

But in case you *know* that xsl:apply-templates has been issued against all
siblings topic elements, then what you actually need is:

count(../../preceding-sibling::topic) + 1 mod 2
this gives an xpath-error:
<xsl:if test="parent::subtopic/parent::topic/position() mod 2 = 0">

Yes, position() cannot be used in place of a node-test.
So I tried this (this should return an empty node-set if the position of
the <topic> is odd)
<xsl:if test="parent::subtopic/parent::topic[position() mod 2 = 0]">

but it doesn't work.

Yes, in a tree every node has at most one parent, therefore the position of
the parent is always 1.
I also tried this:
<xsl:if test="count(parent::subtopic/parent::topic[position() mod 2 = 0])!=0">

but it doesn't work either. can you explain why?

The same as above.


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top