存在某些子项时使用什么xpath选择CDATA内容 [英] what xpath to select CDATA content when some childs exist
问题描述
假设我有一个看起来像这样的XML:
Let's say I have an XML that looks like this:
<a>
<b>
<![CDATA[some text]]>
<c>xxx</c>
<d>yyy</d>
</b>
</a>
我找不到获取某些文字的方法。知道吗?
I can't find a way to get "some text". Any idea?
如果我使用的是 a / b ,则还返回xxx和yyy
a / b / text() 它什么也不返回
If I'm using "a/b" it returns also xxx and yyy If I'm using "a/b/text()" it returns nothing
推荐答案
您实际上无法选择一个CDATA部分:CDATA只是一种告诉解析器避免转义特殊字符的方法,并且您的输入文档在XPath中看起来与以下内容完全相同:
You can't actually select a CDATA section: CDATA is just a way of telling the parser to avoid unescaping special characters, and your input document looks to XPath exactly the same as:
<a>
<b>
some text
<c>xxx</c>
<d>yyy</d>
</b>
</a>
(话虽如此,但如果您使用的是DOM,则某些DOM XPath引擎将无法实现正确设置规范,并将CDATA内容作为与CDATA部分之外的文本分开的文本节点对待。)
(Having said that, if you're using DOM, then some DOM XPath engines fail to implement the spec correctly, and treat the CDATA content as a separate text node from the text outside the CDATA section).
XPath表达式a / b / text()应该选择三个文本节点,其中第一个包含一些文本以及周围的空格。
The XPath expression a/b/text() should select three text nodes, of which the first contains "some text" along with surrounding whitespace.
这篇关于存在某些子项时使用什么xpath选择CDATA内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!