Xpath - 只获取没有其他元素的节点内容 [英] Xpath - get only node content without other elements
问题描述
我有一个 div
元素:
这是一些文字<h1>这是一个标题</h1><div>其他一些内容</div>
我应该使用什么 xpath 表达式来只获取 div
内容而没有他的子元素h1
和 div
//div[not(h1)¬(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)¬(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屋!