XML::LibXML XPath fails to find xmlns:xml="http://www.w3.org/XML/1998/namespace"

Emmanuel Rodriguez <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
Hi,

I have found a strange behavior of XML::LibXML when handling the results 
of an XPath search that includes namespaces using the prefix 'xml'.

If a document defines the following namespace:  
xmlns:xml="http://www.w3.org/XML/1998/namespace" then the XPath 
expression '//namespace::*' will not return that namespace. I have 
attached a test case that shows the problem. The test may seem strange 
as there are more namespaces returned than declared but this is what 
xmllint --shell returns!

I think that this "bug" is a border line case as the prefix 'xml' is 
reserved in the XML specification. Although, I think that it should be 
fine to use xmlns:xml="http://www.w3.org/XML/1998/namespace" in a 
document [See http://www.stylusstudio.com/xmldev/200201/post91720.html].

I tried to use xmllint --shell and a sample C program using the XPath 
API of libxml and they both work fine. So I made some investigation and 
found out that the XS method XML::LibXML::Node::_findnodes() is removing 
the nodes involuntarily through this code:

  if (tnode->type == XML_NAMESPACE_DECL) {
    xmlNsPtr newns = xmlCopyNamespace((xmlNsPtr)tnode);
    if ( newns != NULL ) {
      element = NEWSV(0,0);
      cls = PmmNodeTypeName( tnode );
      element = sv_setref_pv( element,
        (const char *)cls,
        newns
      );
    }
    else {
      continue;
    }
  }

The call to xmlCopyNamespace() is the source of the problem. 
xmlCopyNamespace() uses xmlNewNs() to create a new node, but xmlNewNs() 
has the following assertion:

  if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml")))
    return(NULL);

That's why findnodes() is discarding some results, xmlNewNs() refuses to 
create a namespace node having the prefix "xml".

PS: The same should be true for XML::LibXML::XPathContext::_findnodes().

Emmanuel Rodriguez.

_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
namespaces.t (application/x-troff, 1.9 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.