结合 XPATH 谓词和位置 [英] Combine XPATH predicate with position

查看:20
本文介绍了结合 XPATH 谓词和位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组具有 media-gallery-item 类的 div 元素.
我想选择元素编号 x.

当只选择所有项目时,我得到 5 个结果

$x("//div[@id='content-area']//div[@class='media-gallery-item']")

现在我希望能够选择第 2 项,但我不知道如何正确组合两者:

$x("//div[@id='content-area']//div[@class='media-gallery-item'][2]")$x("//div[@id='content-area']//div[@class='media-gallery-item'和位置() = 2]")

我知道这些没有多大意义,因为它不是实际的 AND,而是更像是:首先根据此过滤,然后选择第二个匹配项".这样做的目的是继续选择事物(例如,在第二个元素中,为我获取 a 元素的 href 属性)

解决方案

分步解答.

要选择具有 div[@id='content-area'] 祖先的元素,它们是各自父元素的第二个子元素,请使用:

//div[@id='content-area']//div[2]

要选择具有 div[@id='content-area'] 祖先的第二个(按文档顺序)div 元素,请使用:

(//div[@id='content-area']//div)[2]

注意区别.

然后,要选择元素,它们是各自父元素的第二个子元素,前提是它们具有media-gallery-item"类,请使用:

//div[@id='content-area']//div[2][@class='media-gallery-item']

要选择元素,它们是具有 media-gallery-item 类的那些子元素(它们各自的父元素)中的第二个元素:

//div[@id='content-area']//div[@class='media-gallery-item'][2]

选择具有 div[@id='content-area'] 祖先的第二个(按文档顺序)div 元素,前提是它具有 media-画廊项目 类:

(//div[@id='content-area']//div)[2][@class='media-gallery-item']

从具有 div[@id='content-area'] 祖先和 media- 的所有 div 元素中选择第二个(按文档顺序)画廊项目 类:

(//div[@id='content-area']//div)[@class='media-gallery-item'][2]

规范引用,正如@Alejandro 所建议的:

<块引用>

谓词过滤节点集相对于轴产生新的节点集.对于每个节点要过滤的节点集,PredicateExpr 被评估为节点作为上下文节点,节点集中的节点数作为上下文大小,以及接近度节点在节点集中的位置相对于轴为上下文位置

http://w3.org/TR/xpath/#predicates ><块引用>

成员的邻近位置一个关于轴的节点集是定义为节点的位置在文档中排序的节点集中如果轴是前轴,则排序并以相反的文档顺序排序如果轴是反向轴

http://w3.org/TR/xpath/#dt-proximity-位置

底线是位置谓词相对于轴起作用.并且您需要括号来明确声明优先级.因此,在计算位置时,将考虑解析descendant-or-self 轴后的节点集,而不是child 轴.

I have a collection of div elements that have the class media-gallery-item.
I would like to select element number x.

When just selecting all items, I get 5 results

$x("//div[@id='content-area']//div[@class='media-gallery-item']")

Now I want to be able to select item number 2, but I can't figure out how to combine the two properly:

$x("//div[@id='content-area']//div[@class='media-gallery-item'][2]")

$x("//div[@id='content-area']//div[@class='media-gallery-item'
                                   and position() = 2]")

I know these don't make a lot of sense since it's not an actual AND, but more like: "filter according to this first, then select the 2nd match". The goal of this is to keep on selecting things (e.g. in the second element, get me the href attribute of the a element)

解决方案

Step-by-step answer.

To select elements with a div[@id='content-area'] ancestor, which are the second children of their respective parents use:

//div[@id='content-area']//div[2]

To select second (in document order) div element with a div[@id='content-area'] ancestor use:

(//div[@id='content-area']//div)[2]

Do note the difference.

Then, to select elements, which are second children of their respective parents, providing they have a class 'media-gallery-item' use:

//div[@id='content-area']//div[2][@class='media-gallery-item']

To select elements, which are the second from those children (of their respective parents) that have a class media-gallery-item:

//div[@id='content-area']//div[@class='media-gallery-item'][2]

To select second (in document order) div element with a div[@id='content-area'] ancestor, providing it has media-gallery-item class:

(//div[@id='content-area']//div)[2][@class='media-gallery-item']

To select second (in document order) from all div element with a div[@id='content-area'] ancestor and a media-gallery-item class:

(//div[@id='content-area']//div)[@class='media-gallery-item'][2]

Spec quotes as suggested by @Alejandro:

A predicate filters a node-set with respect to an axis to produce a new node-set. For each node in the node-set to be filtered, the PredicateExpr is evaluated with that node as the context node, with the number of nodes in the node-set as the context size, and with the proximity position of the node in the node-set with respect to the axis as the context position

http://w3.org/TR/xpath/#predicates

The proximity position of a member of a node-set with respect to an axis is defined to be the position of the node in the node-set ordered in document order if the axis is a forward axis and ordered in reverse document order if the axis is a reverse axis

http://w3.org/TR/xpath/#dt-proximity-position

Bottom line is that the position predicate works with respect to the axis. And you need parenthesis to explicitly declare the priority. Thus not the child axis, but the node-set after resolving descendant-or-self axis will be considered, when calculating position.

这篇关于结合 XPATH 谓词和位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆