Get specific XML node position using XPATH?

Let me start with what I know about position(), but I cannot figure out how to make it work in this context.

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

Here is an example 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 Pond Frozen</title>
            </item>
        </images>
    </entry>
</understanding-individual-question>

I tried several different ways to get what would be the position of this itemnode from another place in XML, but I keep returning errors, nothing or NaN.

Here are three XSLT examples 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>

XPATH node, . , , , position() node! , .

, NaN.

- , ?

+3
1

:

count(preceding-sibling::item) +1
+2

Source: https://habr.com/ru/post/1752866/


All Articles