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

  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 =
  >>> print nsls
  >>> print nsls2

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 ""; 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


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