用于识别未标记文本的 XPath 查询 [英] XPath query to identify untagged text

查看:28
本文介绍了用于识别未标记文本的 XPath 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个 HTML:

<html>
  <head>
  </head>
<body>
  <table>
    <tr>
      <td>
        <h1>title</h1>
        <h3>item 1</h3>
          text details for item 1
        <h3>item 2</h3>
          text details for item 2
        <h3>item 3</h3>
          text details for item 3
      </td>
    </tr>
  </table>
</body>
</html>

我对 XPath 不是很熟悉,但在我看来,没有可以单独匹配文本详细信息"部分的符号.你能确认吗?

I'm not terribly familiar with XPath, but it seems to me that there is no notation which will match the "text details" sections individually. Can you confirm?

推荐答案

使用:

/html/body/table/tr/td/h3/following-sibling::text()[1]

这意味着:获取作为每个table<的子元素的每个tr元素的子元素的每个h3元素的第一个兄弟文本节点/code> 元素是每个 body 元素的子元素,该元素是 html 顶部元素的子元素.

This means: Get the first following sibling text node of every h3 element that is a child of every tr element that is a child of every table element that is a child of every body element that is a child of the html top element.

或者,如果您只知道想要的文本节点是文档中所有 h3 元素 的紧随其后的兄弟节点,那么 XPath 表达式会选择它们:

//h3/following-sibling::text()[1]

这篇关于用于识别未标记文本的 XPath 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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