Re: [xml] help: how to get content of an element via XPath in libxml2



On Wed, Apr 23, 2003 at 04:49:09PM -0700, andy glew amd com wrote:
help: how to get content of an element via XPath in libxml2


Stupid question, but...

I am using libxml2, 2.5.6

I want to be able to use XPath syntax to get the textual content
of a field of a record.  E.g. for something like:

<record>
   <field>value</field>
   <field1> value1 
     <baz/> 
   </field>
</record>

after having parsed the above, I want to be able to do something like

assert(
   document.get_by_xpath("/record/field")
   == "value"
)

and

assert(
   document.get_by_xpath("/record/field1")
   == " value\n     <baz\>\n   "
)


ok, I wrote the above in a C++ style syntax
- in C it might look like


assert(
   0 == strcmp( xmlGetContent(xmlXPathEvalExpression(document,"/record/field")))
                "value" )
)

(with a memory leak... yeah, yeah...)


I must be stupid, or, at least, lacking a good browser,
but I can't figure out how to go from xmlXPathEvalExpression
to any of the xmlNodeGetContent or similar functions.

  You understand that xmlXPathEvalExpression(document,"/record/field"))
returns a node set, that a node set may potentially contain 0, 1 or N 
nodes. A nodeset is not a node, you can't xmlGetContent() the result !
  an xmlXPathEvalExpression returns a xmlXPathObjectPtr
  an xmlXPathObject is a structure with a type
  extracting informations depens on the type returned. Could be a string
for example xmlXPathEvalExpression(document,"string(/record/field)"))
but this actually isn't what you want.
  Try to read about XPath, look a the structures for xmlXPathObjectPtr
check the code of xmlXPathDebugDumpObject() in xpath.c to help if
needed. And subscribe to the list if you need more information,

Daniel

-- 
Daniel Veillard      | Red Hat Network https://rhn.redhat.com/
veillard redhat com  | libxml GNOME XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/



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