Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

what xpath to select CDATA content when some childs exist

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?

If I'm using "a/b" it returns also xxx and yyy If I'm using "a/b/text()" it returns nothing

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

(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).

The XPath expression a/b/text() should select three text nodes, of which the first contains "some text" along with surrounding whitespace.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...