Re: [xml] Python bindings and Xpath and namespaces



On Tue, May 31, 2005 at 10:13:54PM +0100, Nic Ferrier wrote:
When I do this python on a RSS/RDF document:

  doc = libxml2.parseMemory(docstr, len(docstr))
  root = doc.getRootElement()
  nsls = root.nsDefs()

I get a single namespace node and I was expecting a list of 7
namesspaces.

  you get a pointer to the first namespace in the list
not a pointer to the list of declarations on that element, nor
a pointer to the list of in-scope declarations.

Like another poster here I wanted to get a list of the namespace URIs
declared on a document so that I could register them and use xpath
expressions on them.

  I general you can't. Namesapce are not *per document* they are
*per subtree*. So registration at a document level is the wrong
concept to start from.

[...]
So... the value of nsDefs() on the qualified root node is still a
single object and not a list and it's still only the namespace of the
root noot.

So... I'm confused.

  >>> import libxml2
  >>> doc = libxml2.readFile("tst.xml", None, 0)
  >>> root = root = doc.getRootElement()
  >>> nsls = root.nsDefs()
  >>> nsls2 = nsls.next
  >>> print nsls
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
  >>> print nsls2
  xmlns="http://purl.org/rss/1.0/";
  >>>

use next to navigate the list of definitions on a node.

How can I dynamically obtain the namespace declarations of a document
from python? Or can't I?

  You can't as I pointed. You can perfectly have rdf prefix mapped
to "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; in one part of the
documernt and to "foobar" in another part of the document.
  What you can do is extract the list of namespace currently in scope
at one node in the document. It is not a libxml2 problem, it's how
Namespaces are defined. The spec is short, please read it 
   http://www.w3.org/TR/REC-xml-names/

Daniel

-- 
Daniel Veillard      | Red Hat Desktop team http://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]