3

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?

Community
  • 1
  • 1
  • You have just one file, so the position will be 1. Please, provide a more realistic XML document and specify what result (output) you expect. – Dimitre Novatchev Jul 02 '10 at 17:32
  • If you look closely what I'm trying to do is get the position of `item` inside of `images`. You are only seeing one so that you don't have a few hundred lines of redundant XML to look through. :P Also, you *can* get the `position()` in such a situation because I am getting them in another spot in my XSLT. However, the code I'm using there does not work in this situation. –  Jul 02 '10 at 17:38
  • (+1) for your additional explanation -- would be good if you edit and put this into the question. See my answer for a simple and short solution :) – Dimitre Novatchev Jul 02 '10 at 18:18

1 Answers1

2

With your second method use:

count(preceding-sibling::item) +1
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431