css 选择器 'p:nth-of-type(2n)' 的等效 XPath 表达式是什么?



css选择器":第n个类型"的解释,请参阅http://www.w3.org/TR/2001/WD-css3-selectors-20010126/#selectors

是否可以将css选择器"p:nth of type(2n)"转换为等效的XPath表达式?

有人帮忙吗?

下面是(Python)cssselect翻译:

descendant-or-self::*/p[(position() +0) mod 2 = 0 and position() >= 0]

可以简化为

.//p[position() mod 2 = 0]

通过lxml.cssselect:

>>> import lxml.cssselect
>>> lxml.cssselect.CSSSelector('p:nth-of-type(2n)').path
u'descendant-or-self::*/p[(position() +0) mod 2 = 0 and position() >= 0]'
>>> 

您可以尝试我在What XPath中找到的解决方案,从表中选择奇数TR,从第三个开始?

/p[position() mod 2 = 1]

最新更新