Use saxon with python
Question:
I need to process XSLT using python, currently I’m using lxml which only support XSLT 1, now I need to process XSLT 2 is there any way to use saxon XSLT processor with python?
Answers:
There are two possible approaches:
-
set up an HTTP service that accepts tranformation requests and implements them by invoking Saxon from Java; you can then send the transformation requests from Python over HTTP
-
use the Saxon/C product, currently available on prerelease: details here: http://www.saxonica.com/saxon-c/index.xml
A Python interface for Saxon/C is in development and worth a look:
At the moment there is not, but you could use the subprocess module to use the Saxon processor:
import subprocess
subprocess.call(["saxon", "-o:output.xml", "-s:file.xml", "file.xslt"])
Saxon/C release 1.2.0 is now out with XSLT 3.0 support for Python3 see details:
If you’re using Windows:
Download the zip file Saxon-HE 9.9 for Java from http://saxon.sourceforge.net/#F9.9HE and unzip the file to C:saxon
Use this Python code:
import os
import subprocess
def file_path(relative_path):
folder = os.path.dirname(os.path.abspath(__file__))
path_parts = relative_path.split("/")
new_path = os.path.join(folder, *path_parts)
return new_path
def transform(xml_file, xsl_file, output_file):
"""all args take relative paths from Python script"""
input = file_path(xml_file)
output = file_path(output_file)
xslt = file_path(xsl_file)
subprocess.call(f"java -cp C:saxonsaxon9he.jar net.sf.saxon.Transform -t -s:{input} -xsl:{xslt} -o:{output}")
On January 13, 2023, Saxonica has released their own mantained pip package for Saxon 12:
Now all we need is:
pip install saxonche
I need to process XSLT using python, currently I’m using lxml which only support XSLT 1, now I need to process XSLT 2 is there any way to use saxon XSLT processor with python?
There are two possible approaches:
-
set up an HTTP service that accepts tranformation requests and implements them by invoking Saxon from Java; you can then send the transformation requests from Python over HTTP
-
use the Saxon/C product
, currently available on prerelease: details here: http://www.saxonica.com/saxon-c/index.xml
A Python interface for Saxon/C is in development and worth a look:
At the moment there is not, but you could use the subprocess module to use the Saxon processor:
import subprocess
subprocess.call(["saxon", "-o:output.xml", "-s:file.xml", "file.xslt"])
Saxon/C release 1.2.0 is now out with XSLT 3.0 support for Python3 see details:
If you’re using Windows:
Download the zip file Saxon-HE 9.9 for Java from http://saxon.sourceforge.net/#F9.9HE and unzip the file to C:saxon
Use this Python code:
import os
import subprocess
def file_path(relative_path):
folder = os.path.dirname(os.path.abspath(__file__))
path_parts = relative_path.split("/")
new_path = os.path.join(folder, *path_parts)
return new_path
def transform(xml_file, xsl_file, output_file):
"""all args take relative paths from Python script"""
input = file_path(xml_file)
output = file_path(output_file)
xslt = file_path(xsl_file)
subprocess.call(f"java -cp C:saxonsaxon9he.jar net.sf.saxon.Transform -t -s:{input} -xsl:{xslt} -o:{output}")
On January 13, 2023, Saxonica has released their own mantained pip package for Saxon 12:
Now all we need is:
pip install saxonche