views:

53

answers:

1

Let me start by saying I know about position() but I can't seem to figure out how to make it work in this context.

What I'm trying to do is iterate through my body of text and find all images. These will be turned into links that say "Figure 1" and so on. The number is provided by the position() of a corresponding node in a different node-set.

Here is a sample of my XML:

<understanding-individual-question>
    <section id="18" handle="questions">Questions</section>
    <entry id="162">
        <images items="3">
            <item id="215">
                <description mode="normal" handle="winter-frozen-period-for-stile-s-pond" word-count="6">Winter frozen period for Stile’s Pond.</description>
                <file size="73 KB" path="/uploads" type="image/jpg">
                    <filename>lakefrozen-1276880623.jpg</filename>
                    <meta creation="2010-06-18T13:03:43-04:00" width="532" height="479" />
                </file>
                <title mode="normal" handle="stiles-pond-frozen" word-count="3">Stile's Pond Frozen</title>
            </item>
        </images>
    </entry>
</understanding-individual-question>

I've tried a number of different methods to get what would be the position of that item node from another place in the XML but I keep returning errors, nothing or NaN.

Here are three examples of the XSLT I've tried:

<xsl:template match="information//img">
    <xsl:variable name="link" select="substring-after(@src,'uploads/')" />
    <em>(<a rel="figure" href="{@src}">
        <xsl:text>See Figure </xsl:text>
        <!-- Method 1: Returns all as 'NaN' -->
        <xsl:number value="/data/understanding-individual-question/entry/images/item[file/filename = $link][position()]" format="1"/>
        <!-- Method 2: Returns all as '1' -->
        <xsl:for-each select="/data/understanding-individual-question/entry/images/item[file/filename = $link]">
            <xsl:number value="position()" format="1"/>
        </xsl:for-each>
        <!-- Method 3: Returns all as '2' -->
        <xsl:number value="position()" format="1"/>
    </a>.)</em>
</xsl:template>

I've checked my XPATH and it returns the correct node, no problem. However, no matter what I do it never returns the position() of the node! And I can't figure out why.

I tried following this question's solutions but I kept getting NaN.

Anyone have any idea how to do this?

A: 

With your second method use:

count(preceding-sibling::item) +1
Dimitre Novatchev
That did it! You guys rock!