Re: [xml] Get sub-document from xmlDocPtr

On Thu, Jul 17, 2008 at 03:25:20AM -0700, Andrew Hartley wrote:

Andrew Hartley wrote:

Having extracted a sub-document with an XPath expression and now have an
xmlXPathObjectPtr is there a simple way to output the sub-document into a
new xmlDocPtr so that this sub-document can then be saved to disk?

It's ok, I have worked it out:

xmlDocPtr pXmlSubDoc = pXPathObj->nodesetval->nodeTab[0]->doc;

  Note that an XPath object could also be a number, a boolean or a string,
so don't blindly do that dereference and check pXPathObj type first, and
then that the pointer is not null, and then that the node set at least has
one node, and that this node is not a namespace (namespace nodes don't have
pointer back to the document).

  Basically there is tons of checking missing there, and i would prefer
if other people didn't just cut and past that everywhere...
I think it really makes more sense to keep the document pointer around, 
for example it's embedded in the XPath evaluation context. Beware of bad
scoping/API design that happen frequently when you don't have yet a good
vison of the objects manipulated.


Red Hat Virtualization group
Daniel Veillard      | virtualization library
veillard redhat com  | libxml GNOME XML XSLT toolkit | Rpmfind RPM search engine

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