使用XPath获得特定的XML节点的位置?
-
02-10-2019 - |
题
让我首先说我知道 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>
我已经尝试了多种不同的方法来获取它的位置 item
来自XML的另一个位置的节点,但我一直返回错误,什么都没有或 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
.
有人知道该怎么做吗?
解决方案
使用第二种方法使用:
count(preceding-sibling::item) +1
不隶属于 StackOverflow