[Linux Mint*] Xslt in Python
Step-by-step instructions to get you from zero to python xslt "hello world" in 10 minutes**
Installation
- [File Explorer] Download SaxonC-HE from the C/C++, PHP and Python downloads page.
- [File Explorer] Go to the "download" folder from the previous step.
- [File Explorer] Unzip the file from the download step.
- [Terminal] Go into the newly created "libsaxon" folder:
cd libsaxon-HEC-11.4
.
- [Terminal] Copy the SaxonC library:
sudo cp libsaxonhec.so /usr/lib/.
- [Terminal] Copy the Excelsior JET runtime (which handles VM calls):
sudo cp -r rt /usr/lib/.
- [Terminal] Copy the Saxon data folder:
sudo cp -r saxon-data /usr/lib/.
- [Terminal] Set the SAXONC_HOME environment variable:
export SAXONC_HOME=/usr/lib
- [Terminal] Go to the "python saxon" folder:
cd Saxon.C.API/python-saxon
- [Terminal] Update pip:
python -m pip install --upgrade pip
- [Terminal] Install Cython:
pip install Cython
- [Terminal] Install python-saxon:
python saxon-setup.py build_ext -if
- [Terminal] Set PYTHONPATH variable:
export PYTHONPATH=/home/toddmo/Downloads/libsaxon-HEC-11.4/Saxon.C.API/python-saxon
. Replace my username with yours.
Hello World
- [File Explorer] Create the following files, listed below, in a test folder
- [Terminal] Run Hello world:
python saxon-test.py
. Make sure you're in the test folder and your PYTHONPATH env variable is set as indicated above.
saxon-test.py
import os
import saxonc
with saxonc.PySaxonProcessor(license=True) as saxonproc:
xsltproc = saxonproc.new_xslt30_processor()
saxonproc.set_cwd(os.getcwd())
out = xsltproc.transform_to_string(source_file="in.xml", stylesheet_file="test.xsl")
with open("out.xml", "w") as f:
f.write(out)
print(out)
out_contents = saxonproc.parse_xml(xml_file_name="out.xml")
print(out_contents)
test.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
exclude-result-prefixes="#all"
expand-text="yes"
version="3.0">
<xsl:output method="xml" indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="/*" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@*, node()" mode="#current"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
in.xml
<hello>
<world/>
</hello>
Making Paths permanent
- [Terminal] Start editing the bash profile:
nano ~/.bash_profile
- [Nano] Paste the code from "Set PYTHONPATH variable" above
- [Nano] Save the changes: CTRL+X, Y, ENTER
- [Terminal] Start editing the bash profile:
nano ~/.bash_profile
- [Nano] Paste the code from "Set PYTHONPATH variable" above, but it should look like this instead:
export PYTHONPATH="${PYTHONPATH}:/my/other/path"
. Note the $ sign and curly braces.
- [Nano] Save the changes: CTRL+X, Y, ENTER
- [Terminal] Run source:
source ~/.bashrc
- [Desktop] Open a new Terminal
- [Terminal] Test it:
echo $PYTHONPATH
Getting all this going in Visual Studio Code
- [VS Code] [Terminal] The PYTHONPATH variable should be populated in here:
echo $PYTHONPATH
- [VS Code] [Terminal] Hello World should work in here:
python saxon-test.py
- [VS Code] [Extensions] Install
Python
- [VS Code] [Extensions] Install
Pylance
- [VS Code] [Extensions] Install
XSL Transform
. It allows you to do transformations with CTRL+SHIFT+P
- [VS Code] [Extensions] Install
XSLT/XPath for Visual Studio Code
- [VS Code] Copy
saxon-test.py
into a VS Code project
- [VS Code] Rename it to
xslt.py
- [VS Code] [xslt.py] Replace the contents to what is listed below
- [VS Code] You may notice
saxonc
has a red line, with the mouse hover message: Import "saxonc" could not be resolved Pylance (reportMissingImports)
- If you try to call the function, you may get the runtime error:
ModuleNotFoundError: No module named 'saxonc'
- [VS Code] [Run and Debug 🐞 Sidebar] Add
launch.json
.
- [VS Code] [launch.json] Add configurations for a Python file, Django, and / or whatever you need.
- [VS Code] [launch.json] Add the
env
variable to all of your configurations(s): "env": {"PYTHONPATH":${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"}
- [Desktop] Restart VS Code.
- [VS Code] [xslt.py]
import saxonc
should now be working with no error.
xslt.py
import os
import saxonc
from typing import Dict
class Xslt():
@classmethod
def transform(cls, xsl_file: str, xml_file: str, output_file: str, parameters: Dict[str,str]) -> bool:
try:
out = cls.transform_to_string(xsl_file, xml_file, parameters)
with open(output_file, "w") as f:
f.write(out)
return True
except Exception as e:
print(str(e))
return False
@classmethod
def transform_to_string(cls, xsl_file: str, xml_file: str, parameters: Dict[str,str]) -> str:
with saxonc.PySaxonProcessor(license=True) as saxonproc:
xsltproc = saxonproc.new_xslt30_processor()
for parameter in parameters:
xsltproc.set_parameter(parameter, saxonproc.make_string_value(parameters[parameter]))
saxonproc.set_cwd(os.getcwd())
return xsltproc.transform_to_string(source_file=xml_file, stylesheet_file=xsl_file)
def parse_xml(self, xml_file):
with saxonc.PySaxonProcessor(license=True) as saxonproc:
return saxonproc.parse_xml(xml_file_name=xml_file)
* This will probably work on any Ubuntu system.
** I had to cobble these together from multiple locations. Some of the instructions are here. I point this out because I was unable to find this "Getting started" from the home page, the download page, or any other pages quoted. I stumbled upon it in another S/O question. To do all these instructions took hours.