Get a specific XML node's position with XPATH?

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:

    <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">
                    <meta creation="2010-06-18T13:03:43-04:00" width="532" height="479" />
                <title mode="normal" handle="stiles-pond-frozen" word-count="3">Stile's Pond Frozen</title>

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"/>
        <!-- Method 3: Returns all as '2' -->
        <xsl:number value="position()" format="1"/>

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?


With your second method use:

count(preceding-sibling::item) +1

Need Your Help

Export content type in Drupal 7

drupal import drupal-7 export content-type

Is there a way to export a content type from a site and import it to another? I haven't found any modules to do that yet.

C++ unusing using

c++ namespaces using short

Can I undo a command using?