In XPath is there a way to return the child tree including tags? if yes, how?
For example I have the following XML file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>
I want开发者_Python百科 to query book and return the following text:
<title lang="eng">Harry Potter</title>
<price>29.99</price>
Is it possible in XPath? If yes, how do I go about doing that?
Thank you.
If you just want the child nodes of the first book, you can do this:
//book[1]/*
Or a bit more explicit:
/bookstore/book[1]/*
An XPath expression can select the nodes you want, but the serialization is up to the programming language (such as XSLT, C#, PHP, Java) that hosts XPath and its API (such as DOM objects and methods).
This XPath expression:
/*/book[1]/*
selects exactly the wanted nodes:
<title lang="eng">Harry Potter</title>
<price>29.99</price>
The XPath expression above, in plain English, means:
Select all elements that are children of the first (in document order) book
element that is a child of the top element (regardless what is its name) of the XML document.
精彩评论