[lxml-dev] Getting around namespaces

Stefan Behnel stefan_ml at behnel.de
Fri Jan 23 21:23:05 CET 2009


Hi,

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.


> 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.

Stefan


More information about the lxml-dev mailing list