[lxml-dev] Targeted XSL transformations
Stefan Behnel
stefan_ml at behnel.de
Tue Jul 14 21:31:04 CEST 2009
Hi,
Emanuele D'Arrigo wrote:
> I've been using lxml with great pleasure for a few months now and I'd like
> to thank the main authors and the rest of the lxml community for the work
> that has gone into it. From my perspective it's a well done API, with many
> useful features and it's easy to use.
Happy to hear that. :)
> Let's say I have parsed an xml document, representing xhtml data, into an
> ElementTree, and I have applied one or more xsl transformations to add style
> information to it.
>
> Now, let's say I want to add a whole subtree with a top-element of class
> "sub" to it. The subtree to be added might have been partially styled
> already, i.e. because it comes from an xml file that has its own
> xml-stylesheet processing instructions. However, some styling can occurr
> only in the context of the whole tree, i.e. because the document-wide xsl
> transformation file specifically establishes that elements of class "sub"
> must have a yellow background. Furthermore, the same, xsl file asserts that
> if the document contains an element of class "sub" the document's background
> color must be purple rather than blue.
>
> My fundamental question in this context is: how do I avoid re-applying the
> xsl transformation to the whole ElementTree and only apply the bits of the
> transformation that are necessary, due to the change in the tree?
Hmm, to me, this description contains a bit too many uncertainties. If you
can control the input documents, I'd rather try to make the stylesheets
distinct and apply them in a progressing order on a clean input, instead of
trying to figure out which XSLTs you may have to run on a document.
If you can't do that, you can try to make the XSLTs idempotent, so that
they do not break already styled documents or subtrees. But you should
definitely try to let different stylesheets do independent things.
My preferred solution, however: if you can avoid putting any style
information into the (X)HTML document (or at least the style bits that
interfere in multiple XSL docs), and move it out into a CSS file, I think
you can get yourself out of a lot of hassle. Many of the decisions that you
would take in XSLT to put style information in the right places can be left
to the browser's CSS engine.
Stefan
More information about the lxml-dev
mailing list