Reputation: 77
I refer to the post by @sixdiamants from September 2020 (Can Python run XSLT-2.0 transforms and call extension functions written in Python?). at that time it was pointed out that this functionality will be added. I have not found a possibility in the current version, so I would like to know if it works now or if not, when this functionality will be implemented for Python. Unfortunately, for this reason, both the lxml functionalities and SaxonC functionalities have to be used in my overall process.
Example:
XSTL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://www.test.com/my">
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()|comment()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="BOOK">
<xsl:variable name="vExternalPythonFunc">
<xsl:value-of select="my:python_Func('Hello')"/>
</xsl:variable>
<xsl:element name="{name()}">
<xsl:attribute name="{name(@Id)}">
<xsl:value-of select="$vExternalPythonFunc"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Initial XML:
<?xml version="1.0" encoding="UTF-8"?>
<LIST>
<BOOK Id="Hello"/>
<BOOK Id="Hi"/>
</LIST>
Result XML after XSLT-Transformation:
<?xml version="1.0" encoding="UTF-8"?>
<LIST>
<BOOK Id="World"/>
<BOOK Id="Hi"/>
</LIST>
Should it be possible by now to realize this with SaxonC 12.4 for Python, could someone give me a sample Python code? The following is an example which serves as a template and could be expanded.
from saxonche import *
def python_Func(input):
if input=="Hello":
return "World"
else:
return input
if __name__ == "__main__":
sp1 = PySaxonProcessor(license=True)
xml = sp1.parse_xml(xml_file_name='Test.xml')
xslt30_processor = sp1.new_xslt30_processor()
xslt = xslt30_processor.compile_stylesheet(
stylesheet_file='Test.xslt'))
# Maybe more code so that my user Python function can be used in the xslt transformation process.
# ...
#
result_tree = xslt.apply_templates_returning_file(xdm_value=xml, output_file="Result.xml")
Thanks for you're help.
Upvotes: 0
Views: 160