Skip to content
Advertisement

Transforming JSON with XSLT using SaxonEE and Python

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):

JavaScript

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.

JavaScript

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

JavaScript

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement