Mike Yin
Mike Yin

Reputation: 31

xpath select nodes inside comments

Is it possible to select <title lang="eng">Learning XML</title> using xpath within comments

<bookstore>

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

<!--<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>
-->

</bookstore>

Upvotes: 2

Views: 937

Answers (3)

Michael Kay
Michael Kay

Reputation: 163322

There are no nodes inside a comment. Comments just contain text. If you know that the content of a comment is well-formed XML, then you can read the text, pass it to an XML parser, and process the resulting tree. With XPath 3.0, or with processor extensions, you can do all of that within a single XPath expression.

Upvotes: 0

dirkk
dirkk

Reputation: 6218

Using XPath 3.0 you can do so by parsing the comment again using fn:parse-xml. This will result in a normal XML element, which you can now traverse with standard XPath expressions.

parse-xml(/bookstore//comment())/book/title

Upvotes: 3

Madhusudan Joshi
Madhusudan Joshi

Reputation: 4476

You can achieve that as follows :

//bookstore/book/following::comment()[1]

This will result :

<!--<book>
<title lang="eng">Learning XML</title>
   <price>39.95</price>
</book>
-->

Upvotes: 0

Related Questions