I am attempting to write a Python script that transforms JSON to a text file (CSV) with XSLT.
With saxon-ee-10.5.jar, I can successfully perform the desired transformation by running the following command (Windows 10):
java -cp saxon-ee-10.5.jar com.saxonica.Transform -it -xsl:styling.xslt -o:result.csv
How can I achieve the same result by using Python? I have been trying with Saxon-EE/C, but I am not sure if what I want to happen is possible.
Here is an example of what I have tried so far. My XSLT already defines an $in
parameter for the initial.json file, but the PyXslt30Processor.apply_templates_returning_file()
seems to require a call to PyXslt30Processor.set_initial_match_selection()
, of which I am not sure if non-XML files can be passed.
from saxonc import PySaxonProcessor with PySaxonProcessor(license=True) as proc: xslt30proc = proc.new_xslt30_processor() xslt30proc.set_initial_match_selection(file_name='initial.json') content = xslt30proc.apply_templates_returning_file( stylesheet_file='styling.xslt', output_file='result.csv' ) print(content)
Is what I want to accomplish possible with Saxon-EE/C, or should I try techniques of calling Java from Python?
Advertisement
Answer
I think you want to use call_template...
instead of apply-templates, e.g. https://www.saxonica.com/saxon-c/doc/html/saxonc.html#PyXslt30Processor-call_template_returning_file with
xslt30proc.call_template_returning_file(None, stylesheet_file='styling.xslt', output_file='result.csv' )
Using None
as the template name should be identical to using -it
on the command line, i.e. start by calling the template named xsl:initial-template
.
Don’t use xslt30proc.set_initial_match_selection
in that case.
It might, however, help, to set xslt30proc.set_cwd('.')
before the call_template_returning_file
call.