XPath - 除标题中的所有元素

时间:2018-03-11 13:35:39

标签: html xpath web-scraping

尝试找出与XPATHheader内的所有元素匹配的header。我们假设可以通过三个条件检测标题:

  1. 外部标签是header例如。 <header><div.....></header>
  2. 外部标记包含id,其中包含字符串“header”
  3. 外部标记包含class,其中包含字符串“header”
  4. 我的xpath://*[not(ancestor::header)] and //*[not(ancestor::*[contains(@id,"header")])] and //*[not(ancestor::*[contains(@class,"header")])]

    不正确。

    修改 这应该匹配header内的所有链接:

    //*[ancestor::*[contains(@id,"header") or contains(@class,"header") or header]]
    

    现在我想获得除了这些之外的所有元素。

    你知道怎么做吗?

1 个答案:

答案 0 :(得分:2)

单独评估原始XPath中的每个表达式,测试XML文档中是否存在满足这些条件的元素,并返回boolean()

现在你已经将谓词组合起来选择你不想要的特定元素,你只需要否定测试:

//*[not(ancestor-or-self::header) and 
    not(ancestor::*[contains(@id,"header") or contains(@class,"header")])
   ]