[lxml-dev] Getting around namespaces

Ross Vandegrift ross at kallisti.us
Fri Jan 23 22:53:49 CET 2009


On Fri, Jan 23, 2009 at 09:23:05PM +0100, Stefan Behnel wrote:
> Ross Vandegrift wrote:
> > I'm working on XSLT sheets for transforming XML into Python, but I've
> > got something of a hiccup.
> > 
> > I have a collection of identical documents that have unfortunately
> > been tagged with different namespaces.  I know that the semantics
> > haven't changed - someone thought it'd be useful to indicate the
> > version of the generator in the namespace.
> > 
> > This of course makes XSLT a pain - I need duplicate transform sheets
> > that differ only in the namespace configuration.
> 
> ... or you can use local-name(), although that doesn't really make the XSLT
> documents more beautiful.

Wow, I had no idea that function existed.  It merits a single mention
in Learning XML and barely a reference entry in Mastering XSLT.  This
appears to be the only standard XML feature to actually remove a
namespace from an element.

> > Further, namespaces
> > appear to be the one element of XSLT that I can't use a parameter to
> > substitute.
> > 
> > So I'm thinking of pre-processing the XSLT to subsitute the
> > version-specific namespace.  Is there a better way?
> 
> That sounds simple enough. You can replace the namespace declaration in the
> serialised XSLT document before parsing (or walk over the parsed tree and
> replace all namespaces), and then just store one XSLT object per namespace
> in a dict and use the right one depending on the namespace used in the
> document you want to transform.

I've implemented a solution similar to that.  Since I'm the only one
reading and writing my XSL documents, I've got a simple static string
replacement going on.

Do you know if anyone has done any work on transforming general XML
into a Python dict?  That's what my XSLT documents do for a specific
case, and I think if I gave a good go at it (especially now with
local-name()), I could do it.


-- 
Ross Vandegrift
ross at kallisti.us

"If the fight gets hot, the songs get hotter.  If the going gets tough,
the songs get tougher."
	--Woody Guthrie


More information about the lxml-dev mailing list