没有具有相同属性的祖先的属性的 xPath 表达式 [英] xPath expression for attributes that don't have ancestors with same attribute

查看:29
本文介绍了没有具有相同属性的祖先的属性的 xPath 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提取具有属性的元素,而不是单独提取具有相同属性的后代.

使用以下 html:

<div 框>一些文字<div 框>还有一些文字

<div 框>这也需要包括在内

</body></html>

我希望能够提取两个外部

及其后代,包括内部

,但不想单独提取内部

.

我尝试过使用各种不同的表达方式,但我认为我遗漏了一些非常基本的东西.我一直在尝试的主要表达式是: //[@box and not(ancestor::@box) 但这仍然返回两个元素.

我正在尝试使用 Ruby 1.9.2 中的Hpricot"(0.8.3) Gem 来执行此操作,如下所示:

#假设html设置为上面的htmldoc = Hpricot(html)element = doc.search('//[@box 而不是(ancestor::@box)]')# 以下是返回 3 而不是 2元素大小

对此的任何帮助都会很棒.

解决方案

您的 XPATH 无效.您必须解决某些问题才能使用谓词过滤器(例如 []).否则,没有任何东西可以过滤.

此 XPATH 有效:

//div[@box and not(ancestor::div/@box)]

如果元素不能全部保证为

,您可以对元素使用更通用的匹配:

//*[@box and not(ancestor::*/@box)]

I'm trying to extract elements with an attribute, and not extract the descendants separately that have the same attribute.

Using the following html:

<html><body>
  <div box>
    some text
    <div box>
      some more text
    </div>
  </div>
  <div box>
    this needs to be included as well
  </div>
</body></html>

I want to be able to extract the two outer <div box> and its descendants including the inner <div box>, but don't want to have the inner <div box> extracted separately.

I have tried using all sorts of different expressions but think I am missing something quite fundamental. The main expression I have been trying is: //[@box and not(ancestor::@box) but this still returns two elements.

I am trying to do this using the 'Hpricot' (0.8.3) Gem in Ruby 1.9.2 as follows:

# Assuming html is set to the html above
doc = Hpricot(html)
elements = doc.search('//[@box and not(ancestor::@box)]')

# The following is returning 3 instead of 2
elements.size

Any help on this would be great.

解决方案

Your XPATH is invalid. You have to address something in order to use the predicate filter(e.g. []). Otherwise, there isn't anything to filter.

This XPATH works:

//div[@box and not(ancestor::div/@box)]

If the elements aren't all guarenteed to be <div>, you can use a more generic match for elements:

//*[@box and not(ancestor::*/@box)]

这篇关于没有具有相同属性的祖先的属性的 xPath 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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