如何提取标签外的文本xml [英] how to extract text outside tags xml
本文介绍了如何提取标签外的文本xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I want to extract text outside tags. For example,
<body>
This is an exmaple
<p>
blablabla
</p>
<references>
refer 1
refer 2
</references>
</body>
I want to get the text "This is an example" only without text in other tags (p or reference). I tried several methods but does not work. Any1 can help? Big thanks.
解决方案
You must think a text inside a tag like a node. A text node is retrieved using the test node text()
. Example. Given:
<body>
This is an exmaple
<p>
blablabla
<p>
<references>
refer 1
refer 2
<
eferences>
another example
<ody>
XPath:
"/body/text()"
Will retrieve all children text nodes of body
, like "This is an exmaple" and "another example", while:
"/body/text()[1]"
will retrieve only the first one, "This is an exmaple". If you want all the descendant text nodes you can use:
"/body//text()"
or, you want all the text nodes inside first p:
"/body/p[1]//text()"
这篇关于如何提取标签外的文本xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文