使用 XPath:如何排除嵌套元素中的文本 [英] using XPath: how to exclude text in nested elements

查看:27
本文介绍了使用 XPath:如何排除嵌套元素中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一些像下面这样的html

<h1 class="parseasinTitle"><跨度>游戏名称 </span>游戏开发者

有没有办法可以使用 xpath 来获取文本的游戏开发者"部分?通过搜索我尝试过:

//div[@class='unique_id' 而不是(self::h1/span)]

但这仍然给了我完整的文本游戏标题游戏开发者".

解决方案

div[@class = 'unique_id']/text()[not(normalize-space() = '')]

div[@class = 'unique_id']/text()[last()]

取决于上下文.

请注意,您仍然需要修剪生成的文本节点.

if I have some html like the following

<div class=unique_id>    
  <h1 class="parseasinTitle">
    <span> Game Title </span>
 </h1>
 Game Developer
</div>

Is there a way I can use xpath to get JUST the "Game Developer" part of the text? From searching around I tried:

//div[@class='unique_id' and not(self::h1/span)]

But that still gives me the entire text "Game Title Game Developer".

解决方案

div[@class = 'unique_id']/text()[not(normalize-space() = '')]

or

div[@class = 'unique_id']/text()[last()]

depending on context.

Note that you still have to trim the resulting text node.

这篇关于使用 XPath:如何排除嵌套元素中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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