Skip to content

Instantly share code, notes, and snippets.

@martin-honnen
Created July 13, 2023 09:45
Show Gist options
  • Select an option

  • Save martin-honnen/affd53287007d910142706c3428a2e6e to your computer and use it in GitHub Desktop.

Select an option

Save martin-honnen/affd53287007d910142706c3428a2e6e to your computer and use it in GitHub Desktop.
Minimal saxonche sample to transform input XML using XSLT 3.0 to result HTML, taking file names as command line arguments
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Simple group-by example with a composite grouping key from XSLT 3 specification</title>
</head>
<body>
<p>Milano, Italia: 5.26</p>
<p>Padova, Italia: 0.81</p>
<p>Paris, France: 7.4</p>
</body>
</html>
<cities>
<city name="Milano" country="Italia" year="1950" pop="5.23"/>
<city name="Milano" country="Italia" year="1960" pop="5.29"/>
<city name="Padova" country="Italia" year="1950" pop="0.69"/>
<city name="Padova" country="Italia" year="1960" pop="0.93"/>
<city name="Paris" country="France" year="1951" pop="7.2"/>
<city name="Paris" country="France" year="1961" pop="7.6"/>
</cities>
<?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"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output method="html" indent="yes" html-version="5"/>
<xsl:template match="/">
<html>
<head>
<title>Simple group-by example with a composite grouping key from XSLT 3 specification</title>
</head>
<body>
<xsl:for-each-group select="cities/city"
group-by="@name, @country"
composite="yes">
<p>
<xsl:value-of select="current-grouping-key()[1] || ', ' ||
current-grouping-key()[2] || ': ' ||
avg(current-group()/@pop)"/>
</p>
</xsl:for-each-group>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
import sys
from saxonche import *
with PySaxonProcessor(license=False) as saxon:
xslt30_processor = saxon.new_xslt30_processor()
try:
xslt30_processor.transform_to_file(source_file=sys.argv[1], stylesheet_file=sys.argv[2], output_file=sys.argv[3])
except PySaxonApiError as e:
print(f'Transformation failed: {e}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment