使用 XPATH 获取特定 XML 节点的位置?

发布于 2024-09-08 00:36:12 字数 2391 浏览 1 评论 0原文

首先我要说的是我知道 position() 但我似乎不知道如何让它在这种情况下工作。

我想做的是迭代我的文本正文并找到所有图像。这些将变成显示“图 1”等的链接。该编号由不同节点集中相应节点的 position() 提供。

这是我的 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>

我尝试了多种不同的方法来从 XML 中的其他位置获取该 item 节点的位置,但我不断返回错误、无任何内容或 NaN

以下是我尝试过的 XSLT 的三个示例:

<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 并且它返回了正确的节点,没有问题。但是,无论我做什么,它都不会返回节点的 position()!我不明白为什么。

我尝试遵循这个问题的解决方案,但我一直得到NaN

有人知道如何做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一人独醉 2024-09-15 00:36:12

使用第二种方法:

count(preceding-sibling::item) +1

With your second method use:

count(preceding-sibling::item) +1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文