By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

position() question

P: n/a
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:otherwise>
<xsl:text>two</xsl:text>
</xsl:otherwise>
</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,

--
Felix Natter
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
> 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
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.