position() question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Felix Natter

    #1

    position() question

    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:attribut e name="class">
    <xsl:choose>
    <xsl:when test="position( ) mod 2 = 0">
    <xsl:text>one </xsl:text>
    </xsl:when>
    <xsl:otherwis e>
    <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::s ubtopic/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::s ubtopic/parent::topic[position() mod 2 = 0]">

    but it doesn't work. I also tried this:
    <xsl:if test="count(par ent::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
  • Dimitre Novatchev

    #2
    Re: position() question

    > But in a template for 'subsubtopic':[color=blue]
    >
    > <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.[/color]

    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
    [color=blue]
    >
    > this gives an xpath-error:
    > <xsl:if test="parent::s ubtopic/parent::topic/position() mod 2 = 0">[/color]

    Yes, position() cannot be used in place of a node-test.
    [color=blue]
    >
    > So I tried this (this should return an empty node-set if the position of
    > the <topic> is odd)
    > <xsl:if test="parent::s ubtopic/parent::topic[position() mod 2 = 0]">
    >
    > but it doesn't work.[/color]

    Yes, in a tree every node has at most one parent, therefore the position of
    the parent is always 1.
    [color=blue]
    > I also tried this:
    > <xsl:if test="count(par ent::subtopic/parent::topic[position() mod 2 =[/color]
    0])!=0">[color=blue]
    >
    > but it doesn't work either. can you explain why?[/color]

    The same as above.


    =====
    Cheers,

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


    Comment

    Working...