Skip to main content

A python package for the Saxon/C 1.2.1, an XML processor by Saxonica.

Project description

Welcome to Saxonpy!

This's a python wheel package for Saxon, an XML processor from the Saxonica company. Saxon includes a range of tools for XML transformations, XML queries, and schema validations. For this release, we only include the support for the home edition or the open-source version.

When to choose Saxonpy

If you need to use XSLT versions greater than 1.0 like 2.0 and 3.0, then you may use Saxonpy.

  • As of 2021, there are two great python packages already available: xml and lxml . The Python xml seems to be great if you just need to parse the xml documents while lxml seems to offer more options for processing xml files. Because lxml uses libxslt, a C library for transforming xmls, it supports xslt version 1.0 only.

Installation

pip install saxonpy

Usage

I will make references to the Saxonica official documentation a lot from here because they list all the Python APIs with examples for some.
Import all the modules

from saxonpy import *

Import specific modules

from saxonpy import PySaxonProcessor

Now, let's check what 6 other processors are available from Saxon by visiting the Saxonica's site. In the API section, you will find more info about what each processor does.

Next, we will use PySaxonProcessor to check the Saxon version.

from saxonpy import PySaxonProcessor

with PySaxonProcessor(license=False) as proc:
	print(proc.version)

It will print the version like below if your installation is successful.

Saxon/C 1.2.1 running with Saxon-HE 9.9.1.5C from Saxonica

Please note that license=False indicates the open-source version of Saxon.

Example #1

Let's parse a toy XML that was available from Saxonica source code.

from  saxonpy  import PySaxonProcessor

with PySaxonProcessor(license=False) as  proc:
	xml = """\
		<out>
			<person att1='value1' att2='value2'>text1</person>
			<person>text2</person>
			<person>text3</person>
		</out>
		"""
	node = proc.parse_xml(xml_text=xml)
	print("node.node_kind="+ node.node_kind_str)
	print("node.size="+ str(node.size))
	outNode = node.children
	print("len of children="+str(len(node.children)))
	print('element name='+outNode[0].name)
	children = outNode[0].children
	print(*children, sep= ', ')
	attrs = children[1].attributes
	if  len(attrs) == 2:
		print(attrs[1].string_value)

In the output, we will get this.

node.node_kind=document
node.size=1
len of children=1
element name=out
        , <person att1="value1" att2="value2">text1</person>, 
        , <person>text2</person>, 
        , <person>text3</person>, 
value2

As we can see, we can explore the XML node structure, attributes, and many other things if you check more on the APIs.

Example #2

Let's use the XML path processor from Saxonica.

from  saxonpy  import PySaxonProcessor

with PySaxonProcessor(license=False) as  proc:
	xml = """\
		<out>
			<person>text1</person>
			<person>text2</person>
			<person>text3</person>
		</out>"""

	xp = proc.new_xpath_processor()
	node = proc.parse_xml(xml_text=xml)
	xp.set_context(xdm_item=node)

	item = xp.evaluate_single('//person[1]')
	if  isinstance(item,PyXdmNode):
		print(item.string_value)
	# pay attention, Saxon's xdm data type
	value = proc.make_double_value(3.5)
	print(value.primitive_type_name)

The output shows here.

text1
Q{http://www.w3.org/2001/XMLSchema}double

Saxon shows the result given the path.

Example #3

The XSLT processor, #1

from  saxonpy  import PySaxonProcessor

with PySaxonProcessor(license=False) as  proc:
	xsltproc = proc.new_xslt_processor()

	document = proc.parse_xml(xml_text="<out><person>text1</person><person>text2</person><person>text3</person></out>")

	xsltproc.set_source(xdm_node=document)
	xsltproc.compile_stylesheet(stylesheet_text="<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'> <xsl:param name='values' select='(2,3,4)' /><xsl:output method='xml' indent='yes' /><xsl:template match='*'><output><xsl:value-of select='//person[1]'/><xsl:for-each select='$values' ><out><xsl:value-of select='. * 3'/></out></xsl:for-each></output></xsl:template></xsl:stylesheet>")

	output2 = xsltproc.transform_to_string()
	print(output2)

Here is the output that shows the result of trasformation.

<?xml version="1.0" encoding="UTF-8"?>
<output>text1<out>6</out>
   <out>9</out>
   <out>12</out>
</output>

Example #4

Watch out for the not-pythonic way!

from  saxonpy  import PySaxonProcessor

with PySaxonProcessor(license=False) as  proc:
	xsltproc = proc.new_xslt_processor()

	xml = '<a><b>Text</b></a>'
	xslt = '''\
		<xsl:stylesheet version="1.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
		<xsl:param name="a" />
		<xsl:output method='xml' indent='yes' />
		<xsl:template match="/">
		<foo><xsl:value-of select="$a" /></foo>
		</xsl:template>
		</xsl:stylesheet>'''

	document = proc.parse_xml(xml_text=xml)

	#please note the not Python way in the next two lines.
	xdm_a = proc.make_string_value('a was given in the parameter')
	xsltproc.set_parameter('a', xdm_a)

	xsltproc.set_source(xdm_node=document)
	xsltproc.compile_stylesheet(stylesheet_text=xslt)
	output2 = xsltproc.transform_to_string()
	print(output2)

Here we use an XSLT/stylesheet parameter. I just want to highlight that Python String (object) is not the same string that Saxon uses, and the same goes for other types. We need to convert to it by make_string_value. This may be a little exotic and not so pythonic because Saxon is written in Java and cross-compiled for C and then Python. Just be aware of it but you don't need to know anything more about Java or C to use Saxonpy. Please check out the Saxonica's documentation for xquery, schema validation, and others.

Why the with keyword

with is good to clear out the underlying processes when the program exits.

Source code & Development

  • We downloaded the Saxonc HE from the Saxonica's site
  • We have the source code on a github repo to make the wheel packages for Linux and macOS. We have a separate repo for Windows.
  • We use Github actions runners for CI and releases.

Our use cases

Here at the projects of the University of Virginia, we use Saxon for Tibetan cataloging and SolrDb indexing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

saxonpy-0.0.3-cp310-cp310-win_amd64.whl (36.5 MB view details)

Uploaded CPython 3.10Windows x86-64

saxonpy-0.0.3-cp310-cp310-macosx_10_14_x86_64.whl (38.6 MB view details)

Uploaded CPython 3.10macOS 10.14+ x86-64

saxonpy-0.0.3-cp39-cp39-win_amd64.whl (36.5 MB view details)

Uploaded CPython 3.9Windows x86-64

saxonpy-0.0.3-cp39-cp39-macosx_10_14_x86_64.whl (38.6 MB view details)

Uploaded CPython 3.9macOS 10.14+ x86-64

saxonpy-0.0.3-cp38-cp38-win_amd64.whl (36.5 MB view details)

Uploaded CPython 3.8Windows x86-64

saxonpy-0.0.3-cp38-cp38-macosx_10_14_x86_64.whl (38.6 MB view details)

Uploaded CPython 3.8macOS 10.14+ x86-64

saxonpy-0.0.3-cp37-cp37m-win_amd64.whl (36.5 MB view details)

Uploaded CPython 3.7mWindows x86-64

saxonpy-0.0.3-cp37-cp37m-macosx_10_14_x86_64.whl (38.6 MB view details)

Uploaded CPython 3.7mmacOS 10.14+ x86-64

saxonpy-0.0.3-cp36-cp36m-win_amd64.whl (36.5 MB view details)

Uploaded CPython 3.6mWindows x86-64

saxonpy-0.0.3-cp36-cp36m-macosx_10_14_x86_64.whl (38.6 MB view details)

Uploaded CPython 3.6mmacOS 10.14+ x86-64

File details

Details for the file saxonpy-0.0.3-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: saxonpy-0.0.3-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 36.5 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for saxonpy-0.0.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d3bb1c1f5e52b8d1e4eb244ec88b78089fd8970b09d793a67aa7b7864182f5ef
MD5 8cc083d1643ddd18e84750bb903e8f4a
BLAKE2b-256 33462cb0fc0cf5c945907bdf8dd8b4b9b72da9f1e1856eab6b01381806073675

See more details on using hashes here.

File details

Details for the file saxonpy-0.0.3-cp310-cp310-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: saxonpy-0.0.3-cp310-cp310-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 38.6 MB
  • Tags: CPython 3.10, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for saxonpy-0.0.3-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 7e5be1a0b5749661ad814af48bab4adbf687dfb7620a2d6d0503dcc39c3b63fc
MD5 07a9d4bc52bf382a9dddfd1247fb3b57
BLAKE2b-256 62f2860a4648c22b35167142df19ec0c699bead5d47f07b840dd79c74a327c12

See more details on using hashes here.

File details

Details for the file saxonpy-0.0.3-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: saxonpy-0.0.3-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 36.5 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for saxonpy-0.0.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 8c23f2b5b580a76802ef8c15925612b210f367d7340814b46fa9847b3376cdb6
MD5 20f86d10882fe61509f82fe56abcc8bc
BLAKE2b-256 472f021d0b641bfa2e30b9b2ec5a7c69799880490a9b37132a3542dcd353281d

See more details on using hashes here.

File details

Details for the file saxonpy-0.0.3-cp39-cp39-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: saxonpy-0.0.3-cp39-cp39-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 38.6 MB
  • Tags: CPython 3.9, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for saxonpy-0.0.3-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 16430b7e9333d68594c88cfce0d4e379a68791bc9e61bcc47d6410cd4fd1cca9
MD5 c434ef1fc20c497cf7bbf2bfea2fd90b
BLAKE2b-256 f78a161e8c0e1354e088fa939326a1482c42cb4be88e8dc710fd9fea4a47f742

See more details on using hashes here.

File details

Details for the file saxonpy-0.0.3-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: saxonpy-0.0.3-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 36.5 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for saxonpy-0.0.3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 d9fc4ab064d1425c78ae7335b2877759a844cb1a909c4e71c01340074e09331c
MD5 fc7c69f31c08b265af971fbd2edf5a75
BLAKE2b-256 2aa950c3831d7b74d5123488f821e5686d14ccd96935767e396c844351c130b4

See more details on using hashes here.

File details

Details for the file saxonpy-0.0.3-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: saxonpy-0.0.3-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 38.6 MB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for saxonpy-0.0.3-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 8a79cc6076eb08d620b38b4c74fbcf83357b35a552ed79153e6390f167750e57
MD5 7205e728dcf454e36b1d0d722073c21e
BLAKE2b-256 bdc95ce7104a2a1f9a31ced1d634a0b0692706073ca485ca19da81c4b1112150

See more details on using hashes here.

File details

Details for the file saxonpy-0.0.3-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: saxonpy-0.0.3-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 36.5 MB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.9

File hashes

Hashes for saxonpy-0.0.3-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 9b5f118707302094c4fe4629d03310a4bbcd8c5fd68adb6856afe34cfe73f733
MD5 ef23ba75b1ca1d3ff941337a7ff30fdc
BLAKE2b-256 7b74b14c2af06a91e08b9d1e1f7800ef33d13124eac946032cc969ddfd217dd0

See more details on using hashes here.

File details

Details for the file saxonpy-0.0.3-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: saxonpy-0.0.3-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 38.6 MB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

File hashes

Hashes for saxonpy-0.0.3-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 19d7c4f26add90730f729487d87433016dfe9e07f7bc36b05fdd0944faa3485f
MD5 0658695a6e1a95ce0546ca327eec7786
BLAKE2b-256 ff7f933fe13fe747fadeda3ac2306ebc876c2757e8c75677eb2c7f19a40d2550

See more details on using hashes here.

File details

Details for the file saxonpy-0.0.3-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: saxonpy-0.0.3-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 36.5 MB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.3 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.8

File hashes

Hashes for saxonpy-0.0.3-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 c042cd9b021f2c9a11495ab97739cf6aa342d8a118e9dffc94dc33b6db1eac39
MD5 fd65cde9c435e8e9fc8b697b33bdf89c
BLAKE2b-256 d887a2f7b4c760d0269732e039863bf7b5ad436bd48b61871e93c80383c4efb5

See more details on using hashes here.

File details

Details for the file saxonpy-0.0.3-cp36-cp36m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: saxonpy-0.0.3-cp36-cp36m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 38.6 MB
  • Tags: CPython 3.6m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.3 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.15

File hashes

Hashes for saxonpy-0.0.3-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 81e6fda0117c4bcf817c35778afb95f30d56149a9a0e4e77ea638b8bb836f720
MD5 2d347929452645a24aba0e4da78999d7
BLAKE2b-256 37a2bf360ba1d9eac915204c2f10ad89161166c01d0e6ef4913957338508389a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page