[lxml-dev] setting xslt output encoding with lxml

Stefan Behnel stefan_ml at behnel.de
Sat Apr 4 21:01:44 CEST 2009


Hi,

chris hoke wrote:
> (hope this is the right list for my question)

Yes.


> To set the XSL output encoding I normally use <xsl:output encoding="..."/>
> in the stylesheet.
> 
> At least in the Java based XSLT processors it is possible to set some
> attributes of xsl:output from the "outside" meaning when initializing or
> starting the transformation. So it is possible for example to overwrite any
> encoding specified in <xsl:output... with a different encoding.

lxml.etree does not currently support this.


> Is there any way to do this with LXML?

You can parse the stylesheet with the normal XML parser, change the
xsl:output element according to your needs, and pass the result to XSLT().

Note that you can use

	iterparse(the_file, tag="{...XSL NS...}output")

to update the element while parsing.


> BTW, the example on http://codespeak.net/lxml/xpathxslt.html#xslt
> 
>>>> xslt_tree = etree.XML('''\
> ... <xsl:stylesheet version="1.0"
> ...     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"<http://www.w3.org/1999/XSL/Transform>>
> 
> ...     <xsl:template match="/">
> ...         <foo><xsl:value-of select="$a" /></foo>
> ...     </xsl:template>
> ... </xsl:stylesheet>''')
>>>> transform = etree.XSLT(xslt_tree)
>>>> f = StringIO('<a><b>Text</b></a>')
>>>> doc = etree.parse(f)
> 
> seems to miss an <xsl:param name="a"/> parameter. I have not checked if it
> works without it but I guess it would be good style to declare any incoming
> parameters if not for setting a default value, would it not?

Yes, thanks for catching that.

Stefan


More information about the lxml-dev mailing list