position() doesn't return expected value

Discussion in 'XML' started by Yereth, Jan 19, 2004.

  1. Yereth

    Yereth Guest

    Hi again,

    still working on my xslt program and I ran into another problem. There is a
    file which a contains the following structure:

    <key>1</key>
    <dict>
            ...
    </dict>
    <key>2</key>
    <dict>
            ..
    </dict>

    When I am in a "dict" tag I want to get the value from the key before it,
    because they belong to eachother.. (don't ask me why it's constructed like
    this, wasn't my work) Now I wrote the following code which should do the
    job:

    <xsl:template match="plist/dict">
            <xsl:element name="playlist">
                    <xsl:for-each select="child::dict/dict">
                            <xsl:call-template name="track"/>
                    </xsl:for-each>
            </xsl:element>
    </xsl:template>

    <xsl:template name="track">
            <xsl:element name="track">
                    <xsl:variable name="pos" select="position()"/>
                    <xsl:apply-templates select="preceding-sibling::key[position()
    $pos]"/>
                    <xsl:for-each select="child::key">
                            <xsl:call-template name="key_value"/>
                    </xsl:for-each>
            </xsl:element>
    </xsl:template>

    Only, once pos has gained a value, it doesn't seem to get a new value in the
    next round of the "for-each" loop.. is this a bug or am I making a mistake
    in my thinking? Any help would be appreciated!

    Thanks in advance,

    Yereth
     
    Yereth, Jan 19, 2004
    #1
    1. Advertising

  2. Yereth

    Peter Flynn Guest

    What I just said. To find the element position, count the preceding
    siblings of the same name, eg

    <xsl:if test="count(preceding-sibling::dict)=0">

    means "this is the first dict child of its parent".

    Personally I would have called the existing function node-position()
    and kept position() to mean what it always meant before: position
    within the elements; but I think the design team assumed everyone
    would be processing without a DTD or Schema, and would therefore
    not be handling only element nodes. But that's life.

    ///Peter
     
    Peter Flynn, Jan 20, 2004
    #2
    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. Yereth
    Replies:
    2
    Views:
    521
    Yereth Jansen
    Jan 20, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    835
    Keith Thompson
    Mar 6, 2005
  3. Replies:
    4
    Views:
    552
    James Kanze
    Jul 22, 2008
  4. Andrew Poulos

    Return inex position of value in multi dim array

    Andrew Poulos, Mar 2, 2005, in forum: Javascript
    Replies:
    5
    Views:
    138
  5. Andrew Poulos

    DOM created image expected position shifts

    Andrew Poulos, May 23, 2005, in forum: Javascript
    Replies:
    2
    Views:
    112
    Martin Honnen
    May 23, 2005
Loading...

Share This Page