There is a problem trying to pass string parameters to an xslt 2.0 script, pure integer parameter work fine in this example, but a string like "value"
or even "w1234"
will fail to be accessable through $my_param
in xslt.
Is this a known problem? (Gemini could not help, neither typical string() modifications in the code.) How can I make passing string parameters into XSLT 2.0 work?
Python:
from lxml import etree
xml_doc = etree.parse('input.xml')
xslt_doc = etree.parse('my_stylesheet.xslt')
transform = etree.XSLT(xslt_doc)
result_tree = transform(xml_doc, my_param='value')
result_tree.write('output.xml', pretty_print=True)
XSLT-Stylesheet:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<root>
<param><xsl:value-of select="$my_param"/></param>
</root>
</xsl:template>
</xsl:stylesheet>
XML:
<?xml version="1.0" encoding="utf-8"?>
<root>
<data />
</root>
Expected:
<root>
<param>value</param>
</root>
Actual:
<root>
<param/>
</root>
If my_param
is 123
(or any other int, even 10-2
which results in <param>8</param>
) does work fine. String values seem to get lost by executing the xslt script.
You can use etree.XLST.strparam
:
from lxml import etree
xml_doc = etree.parse('input.xml')
xslt_doc = etree.parse('my_stylesheet.xslt')
transform = etree.XSLT(xslt_doc)
result_tree = transform(xml_doc, my_param=etree.XSLT.strparam('value'))
result_tree.write('output.xml', pretty_print=True)
Output:
<root>
<param>value</param>
</root>