Re: [xml] How to use XPath to retrieve the "leaf" item?



On Wed, Sep 12, 2012 at 12:47:42PM -0400, Jason Viers wrote:
On 9/12/2012 11:58, Ramon F Herrera wrote:
which is easy enough to parse. Next, I made a discovery: If I add
an extra function at the end of the string, I obtain exactly what
I need:
XPath query: /table/properties/length/text()
Result: 62.42
That function works properly with all XPath implementations that I
have tried, EXCEPT with libxml.
Result (1 nodes):
= node "text": type 3
I need the last step, after the node set. How do I retrieve that end item?

You're on the right path, you just need xmlNodeGetContent on that node.
http://xmlsoft.org/html/libxml-tree.html#xmlNodeBufGetContent

It would be best to xpath for "/table/properties/length" and use
xmlNodeGetContent on the element, though, in case there's more than
one text node or the text isn't directly in a text node child of
length, such as:
<length units="ft">62<![CDATA[.42]]></length>

"/table/properties/length/text()" would give you "62", but
"/table/properties/length" gives 62.42

  Note that the XPath data model expect text and CDATA nodes to be
merged, so when you parse XML for XPath consumption you should give
the XML_PARSE_NOCDATA flag to be sure you will get the result intended
by the spec.

 Another simpler solution is just the XPath expression

   "string(/table/properties/length)"

which will return a string with the content (if found)

Daniel

-- 
Daniel Veillard      | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
daniel veillard com  | Rpmfind RPM search engine http://rpmfind.net/
http://veillard.com/ | virtualization library  http://libvirt.org/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]