XPath - 如何选择文本 [英] XPath - how to select text

查看:23
本文介绍了XPath - 如何选择文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得The quick brown fox.在以下文档中:

How do I get The quick brown fox. in the following document:

<a>
   <b>
      Hello
      <c/>
      World
   </b>
   The quick brown fox.
</a>

推荐答案

正如评论中所讨论的,在处理混合内容时,了解是否仅保留或去除空白文本节点很重要.

As discussed in comments, when dealing with mixed content is important to know whether white space only text nodes are being preserved or stripped.

通用解决方案:

/a/text()[normalize-space()][1]

含义:a根元素的第一个不是空白的纯文本节点子

Meaning: first not white space only text node child of a root element

其他可能性:

/a/text()[last()]

含义:a 根元素的最后一个文本节点子节点

Meaning: last text node child of a root element

这篇关于XPath - 如何选择文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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