Re: [xml] handling namespaces



On Sat, May 11, 2013 at 12:17:57AM -0400, Eric Levy wrote:
Hello,

I am struggling to understand how libxml2 is meant to handle
namespaces.  Most recently, I created an in-memory document using
the public API that causes libxslt to hang on
xlstCopyNamespaceList() when I call xsltApplyStylesheet().

I am developing a small application that has a feature similar to
XInclude.  An element in one document causes the inclusion of a
subtree of another document.  I use the xmlCopyNode() routine to
obtain a subtree suitable for being inserted into another document,
and I had hoped that the library layer handles namespaces
transparently, but it seems not so.  I should say that I also tried
xmlDocCopyNode(), but I saw no difference in behavior.  I observe
that the ns field of xmlNode is  only populated if the node has a
namespace declaration in the original document.  Further, the

  That is completely normal. if the element had no namespace
in the source document, why should libxml2 'invent' a namespace for it.

resulting document I created is corrupt, as evidenced from the
observation that when it is used as a stylesheet in a
transformation, the transformation method never returns, but rather
goes into xsltCopyNamespaceList() indefinitely.

Perhaps someone can address the following issues:

1) In some xmlDoc, how do I determine the namespace of a particular
xmlNode, following XML rules for inheritance?

   Check that the type of the node is Element or Attribute, then
after casting to an xmlAttrPtr is needed the ns points to the namespace
declaration, hosted either on the local Element or one of its ancestors.

2) How do I copy xmlNode instances across documents so as to correct
for namespaces?

  xmlStaticCopyNode() which  is called by xmlCopyNode() tries to do the
right thing:

            /*
             * Humm, we are copying an element whose namespace is
             * defined
             * out of the new tree scope. Search it in the original tree
             * and add it at the top of the new tree
             */

  
3) Is the behavior I described a bug in the library, or my misuse of it?

  Not completely sure.

Daniel

-- 
Daniel Veillard      | Open Source and Standards, Red Hat
veillard redhat com  | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | virtualization library  http://libvirt.org/


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