加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – 仅使用XPath选择第一个实例?

发布时间:2020-12-16 08:00:15 所属栏目:百科 来源:网络整理
导读:我正在解析一些这样的XML: root some_gunk/ dupe_node ... stuff I want ... /dupe_node bits_and_pieces/ other_gunk/ dupe_node ... stuff I don't want ... /dupe_node more_gunk//root “// dupe_node”的XPath将给我两个dupe_node的实例来播放。我只想
我正在解析一些这样的XML:
<root>
    <some_gunk/>
    <dupe_node>
        ...
        stuff I want
        ...
    </dupe_node>
    <bits_and_pieces/>
    <other_gunk/>
    <dupe_node>
        ...
        stuff I don't want
        ...
    </dupe_node>
    <more_gunk/>
</root>

“// dupe_node”的XPath将给我两个dupe_node的实例来播放。我只想穿过第一个。我可以用XPath这样做吗?

/descendant::dupe_node[1]

// dupe_node [1]通常是错误的,虽然它在这种特殊情况下产生相同的结果。 See docs:

The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.

给出以下XML:

<foo>
    <bar/>
    <foo>
        <bar/>
    </foo>
</foo>

// bar [1]将产生两个节点,因为两个栏都是其各自父项的第一个子节点。

/ descendant :: bar [1]将只给出一个节点,它是文档中第一个条。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读