Xpath - 只获取没有其他元素的节点内容 [英] Xpath - get only node content without other elements

查看:36
本文介绍了Xpath - 只获取没有其他元素的节点内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 div 元素:

这是一些文字<h1>这是一个标题</h1><div>其他一些内容</div>

我应该使用什么 xpath 表达式来只获取 div 内容而没有他的子元素h1div

//div[not(h1)&not(div)]

类似的东西?我想不通

解决方案

获取div的字符串值使用:

string(/div)

这是(顶部)div 元素的所有文本节点的串联.

要选择 div 的所有文本节点后代,请使用:

/div//text()

要仅获取作为 div 的直接子节点的文本节点,请使用:

/div/text()

最后,获得 div 的第一个(希望也是唯一的)非空白文本节点子节点:

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

I have an div elemet:

<div>
   This is some text
   <h1>This is a title</h1>
   <div>Some other content</div>
</div>

What xpath expression should I use to only get the div content without his child elements h1 and div

//div[not(h1)&not(div)]

Something like that? I cannot figure it out

解决方案

To get the string value of div use:

string(/div)

This is the concatenation of all text nodes that are descendents of the (top) div element.

To select all text node descendents of div use:

/div//text()

To get only the text nodes that are direct children of div use:

/div/text()

Finally, get the first (and hopefully only) non-whitespace-only text node child of div:

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

这篇关于Xpath - 只获取没有其他元素的节点内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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