Getting XSLT Parameters from Stylesheet when imported
Hi,
thanks to the awesome lxml module, parsing and transforming XML is
really easy in Python now. So a big thank you to all the developers for
their work and contribution, I like lxml A LOT! :-)
Hope this question hasn't been asked before, but haven't found an
answer in the doc yet.
Let's say, I have a customization layer of the DocBook stylesheets
which looks like this:
--------[ mydocbook.xsl ]--------
xslttree = etree.parse("mydocbook.xsl") params = xslttree.xpath("/xsl:*/xsl:param[@name='admon.graphics']", namespaces={"xsl":"http://www.w3.org/1999/XSL/Transform"})
So far, so good. However, if I disable the above parameter in the customization layer and want to get the original value (in our example "0") the above code doesn't work. Of couse, the xslttree just "sees" only the elements, but does not "resolve" any imports. The question is: How can I access the "imported" parameter anyhow? Do I need to traverse the whole import tree? Or is there any other preferred or recommended method or trick which I should use? Probably I need the equivalent of a xinclude() method for XSLT. ;) Any pointers or hints are much appreciated! :-) Thanks! -- Gruß/Regards, Thomas Schraitle
Hi,
On Mon, 2 Jul 2012 16:10:19 +0200
Thomas Schraitle
[...] In my customization layer, I've overwritten this parameter and set it to "1". To get the above parameter, this can be done with little XPath magic:
xslttree = etree.parse("mydocbook.xsl") params = xslttree.xpath("/xsl:*/xsl:param[@name='admon.graphics']", namespaces={"xsl":"http://www.w3.org/1999/XSL/Transform"})
So far, so good. However, if I disable the above parameter in the customization layer and want to get the original value (in our example "0") the above code doesn't work. Of couse, the xslttree just "sees" only the elements, but does not "resolve" any imports.
The question is: How can I access the "imported" parameter anyhow? Do I need to traverse the whole import tree? Or is there any other preferred or recommended method or trick which I should use? Probably I need the equivalent of a xinclude() method for XSLT. ;)
Any pointers or hints are much appreciated! :-)
I've found a solution, although it's pretty awkward.
It works as follows: The function below creates a temporary stylesheet
object importing the original stylesheet. The only purpose of this
temporary stylesheet is to return the value of our wanted parameter.
If there is no such parameter, we return None.
---------------
def getxsltparam(xsltfilename, param):
"""HACK: Returns the value of the parameter 'param' from the given
stylesheet.
This may be an expensive call, if there is no catalog resolution
available. It just creates a stylesheet in memory and applies a
dummy document to it. The stylesheet just returns the value of
the parameter as text output.
"""
xslt="""
participants (1)
-
Thomas Schraitle