html - XPATH 子字符串前后返回两个 html 标记之间的文本
问题描述
<h4>Start here</h4>
<p>Text stuff 1</p>
<p>Text stuff 2</p>
<h4>Stop here</h4>
我试图在一个查询之前和之后使用子字符串来获取具有以下文本的 h4 标记之间的所有内容:“从这里开始”,然后在文本“在此停止”之前停止
所以我的正确查询将返回文本:
Text Stuff 1
Text Stuff 2
无论我使用此查询的任何变体,它似乎都不起作用:
substring-before(substring-after(//h4[contains, 'Start here'),'//h4[contains, 'Stop here')
解决方案
您想要直接跟随特定的节点<h4>
,其中“直接跟随”可以表示为“前面的第一个<h4>
是我们开始的那个”(当然,所讨论的节点不是<h4>
本身)。
这个表达式(*)
//h4[. = 'Start here']/following-sibling::*[not(self::h4) and preceding-sibling::h4[1][. = 'Start here']]
从此文档中选择
<body>
<h4>Not relevant</h4>
<p>Other stuff</p>
<h4>Start here</h4>
<p>Text stuff 1</p>
<p>Text stuff 2</p>
<h4>Stop here</h4>
<p>Other stuff</p>
</body>
这些节点
<p>Text stuff 1</p>
<p>Text stuff 2</p>
您可以在主机应用程序中提取/加入它们的文本值。
(*)也可以写成//*[not(self::h4) and preceding-sibling::h4[1][. = 'Start here']]
,但是必须检查更多节点,即文档中的所有节点,而不是仅检查一个特定节点的下同级轴。
推荐阅读
- react-native - React Native 应用程序在关闭模式或操作表后神秘地冻结
- python - 使用 smtp 从 Python 发送电子邮件时如何添加主题和变量?
- python - 寻求理解 Python Synta 代码错误
- swift - 使用 RX 只调用一次 refreshtoken WS
- r - 检查列中的数字是否按某个值升序(R数据框)
- python - 在 Python 中将 Dataframe 转换为嵌套字典
- css - 防止长列表项移动到项目符号下方
- python-3.x - Python3如何识别服务器使用的HTTP版本
- javascript - 如何摆脱 JS 控制台中未定义/未定义的消息
- reactjs - 如何在antd的表单提交上获取子值?