A python package for the SaxonC-PE 12.0, an XSLT 3.0, XQuery 3.1, XPath 3.1 and Schema Validator processor by Saxonica.
Project description
Welcome to SaxonC PE!
This is the official Saxonica Python wheel package for Saxon, an XML document processor. SaxonC includes a range of tools for XML transformations, XML queries, and schema validations.
The SaxonC release comes in separate wheels for the three product editions, namely: saxonche (HE: open-source), saxoncpe (PE: professional edition) and saxoncee (EE: enterprise edition). SaxonC-PE and SaxonC-EE are commercial products that require an activation license.
When to choose SaxonC
The main reason for using SaxonC in preference to other XML tools available for Python is that it supports all the latest W3C standards: XSLT 3.0, XPath 3.1, XQuery 3.1, and XSD 1.1. It even includes experimental support for the draft 4.0 specifications currently under development.
Installation
pip install saxoncpe
Usage
from saxoncpe import *
Import specific modules
from saxoncpe import PySaxonProcessor
Now, let's check what processors are available see our docuemntation: Saxonica's site. In the API section, you will find more information about what each processor does.
Next, we will use PySaxonProcessor
to check the Saxon version.
from saxoncpe import PySaxonProcessor
with PySaxonProcessor(license=False) as proc:
print(proc.version)
It will print the version like below if your installation is successful.
SaxonC-pe 12.0 from Saxonica
Please note that license=False
requests the open-source version of Saxon, whereas license=True
requests the commercial product. A license file is required for this to work. You can use the SAXONC_HOME environment variable to locate the license file or install the file next to the SaxonC library.
Example #1
Let's parse a toy XML.
from saxoncpe 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 saxoncpe 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 saxoncpe import PySaxonProcessor
with PySaxonProcessor(license=False) as proc:
xsltproc = proc.new_xslt30_processor()
document = proc.parse_xml(xml_text="<out><person>text1</person><person>text2</person><person>text3</person></out>")
xsltproc.set_source(xdm_node=document)
exeuctable = 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 = exeuctable.transform_to_string()
print(output2)
Here is the output that shows the result of transformation.
<?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 saxoncpe import PySaxonProcessor
with PySaxonProcessor(license=False) as proc:
xsltproc = proc.new_xslt30_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)
executable = xsltproc.compile_stylesheet(stylesheet_text=xslt)
output2 = xsltproc.transform_to_string()
print(output2)
Here we use an XSLT/stylesheet parameter. Note that the Python string is not the same string that Saxon uses, and the same goes for other types. We need to convert it by calling make_string_value
. This is because there isn't an exact match between the type system used by XSLT (called XDM) and Python's data types: for some types like strings and booleans there is a very close correspondence, but for numbers, dates, URIs etc it's less exact.
Please check out Saxonica's documentation for xquery
, schema validation, and others.
Why the with
keyword?
Using with
assures that the underlying processes are cleared out when
the block finishes.
Source code & Development
- Source code is available here: Saxonica's site
Acknowledgement
To create this wheel project we had help from the Saxonpy wheel package, which is a third-party project created by github repo.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for saxoncpe-12.0.0-pp39-pypy39_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b1726e49d19035989e74ca0cb89f5119c9b738e50a55bb7afd2f17542fa191d |
|
MD5 | 919903589a34a6123b4619ad215ad995 |
|
BLAKE2b-256 | f304f4f717ed4b57cda38fa2ca0ae9d014f05942b425941a862c42efa5818f97 |
Hashes for saxoncpe-12.0.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1d22af0b80d9f7f4fd7e3c6697ae85ed1c306ec0d217115a77d4b1d75d8d275 |
|
MD5 | ab02d0187fdaa593b44d9585b33ee897 |
|
BLAKE2b-256 | 637325b4e18f7f09629ab506b2212d73ea88942c4ed4dbaf6bd87aebc5f2a6b2 |
Hashes for saxoncpe-12.0.0-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c5b833f2f048820362c11ed562b3dc044930d92ec8d90a203351c41bd4aade0 |
|
MD5 | a8b1264e627b840d9fa0b1f4c497c90a |
|
BLAKE2b-256 | 846a87c23a56dd72dddd1ee10fa802a55b011fea071b463e6a457aa1add00d9b |
Hashes for saxoncpe-12.0.0-pp38-pypy38_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39bdfad85269d0d6b2df9bdd1ba29f15d1415f41c6fcdef4822c5bc1e2ef65ca |
|
MD5 | 0e4baf643ba69adf9ce4ed0285f66a5d |
|
BLAKE2b-256 | 7c9fd8f4157afbf407b1a3c380ca580047dbde7703b1a9ed6b25c39d8a19496e |
Hashes for saxoncpe-12.0.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1485e3dded35dcc5ff53d957da525853010a081a81de6b04287d051d35c3c5f0 |
|
MD5 | 4404b7358f733e14413bf4ad722c8101 |
|
BLAKE2b-256 | c5b8473bcf9de5f197a87feff3be95e2de5f636550abce10a614177d29cb6483 |
Hashes for saxoncpe-12.0.0-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c699fa0995b79ac30377d6f21ce340e11dcd3bc048cd527dd4e00a54ad7a4582 |
|
MD5 | cd1e28d3b162187cae15b37ba2c83e41 |
|
BLAKE2b-256 | 7f0704faa33015d89620be74c2719e9faf72059087794f90f4bf1d683840c812 |
Hashes for saxoncpe-12.0.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcff069a4e98e8fbe073c033f1bed3d875924463e5f064bee529fd121b92a19d |
|
MD5 | 026d69669dd1aae502252158edd4fdb4 |
|
BLAKE2b-256 | 579608b14ac7b7cb6a3a7d99842ea1a73012857028b52a67289eb4f10b15022f |
Hashes for saxoncpe-12.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8c5ecd08131bdf1428b0c6602049a8a86b6aff9b1176294b88251797449ecb3 |
|
MD5 | 8e7825c57c3a7178da21617170aded22 |
|
BLAKE2b-256 | 38240ba8c8263712736e62daf6218ad056ce4cdc47e725f24455112e5c110073 |
Hashes for saxoncpe-12.0.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd38f3ec15a27c1a954e95b6ecd98a1ae85ef400744c8e2b1dcba605e20abf14 |
|
MD5 | d1946e488ebd91717bde9c4b17d2f148 |
|
BLAKE2b-256 | 4f627e196cbc5046a0de033a24768f6d85a0c1d62b16ee7c6e30090922022404 |
Hashes for saxoncpe-12.0.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 480b60a8dabb8cf5f1f99c98b552babedaa6f682ac2f927b38b77d9790fb00a8 |
|
MD5 | 154050998dc4ccbcef28b5d47bfb99ea |
|
BLAKE2b-256 | 279e5be050f4f3eee53e8c3903806461092721312de3e3be040085c0ceea6f28 |
Hashes for saxoncpe-12.0.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48ad17867966d406ff9bde7f3a31440e6187de14cc2e806b93bece25de2e730c |
|
MD5 | 108c995244c43ba7bc5f25636b91514c |
|
BLAKE2b-256 | 346fe34078a8df59a00e5da42ffe735e42aa5b0d7d6f8d8f31aba7048da43518 |
Hashes for saxoncpe-12.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9935974415238407efdb36b0eb760642b7c791c6c64b765790c90d5f1196f7c3 |
|
MD5 | a4aadf92b66bb1dd58e5cf8ca8fb878d |
|
BLAKE2b-256 | b1809f24e0a01588fd7929971a8db911fbf778baa2f6692208ff5249d48d029a |
Hashes for saxoncpe-12.0.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16b8a3627f05fbddcfa72567fdd183b34aea8f286de4a6e11701ce896c551985 |
|
MD5 | 1a4f94acc98491a101f444237df72fa4 |
|
BLAKE2b-256 | d674ecf155221d6da65df270355238fdd8f58040b4e57ebcde7e9a4db4f041e3 |
Hashes for saxoncpe-12.0.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f95e745fbb67ede19604e73acf15e8f5f3261fec4c41c16c8d8f5f73facd4828 |
|
MD5 | 4b8212c4d8148c230e9ae9f69de8b961 |
|
BLAKE2b-256 | cab3f60b9699ba01bd9719c2bd5927a989fa723043c0f6e0fa655e3ed030632f |
Hashes for saxoncpe-12.0.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fa07ef6e4e4586859bf22b63f9c16149858e41a26e040be4373983126e133fa |
|
MD5 | ada9f83367884f168c0d040c0f54d75a |
|
BLAKE2b-256 | b11269ef67311e3e15779561cb1d89478bfa83884aa0d07d8d5df287b02472f0 |
Hashes for saxoncpe-12.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0d512869860ba3fb8632f3e597984d4b1e662113a6b1a4c8fded333079f3f99 |
|
MD5 | 5847da7bd6093958842d537bd2ee8df0 |
|
BLAKE2b-256 | cb9334c4b5fafad986d0962f6acd6992f96714f2466156427c8f9a01570a5197 |
Hashes for saxoncpe-12.0.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07306c25147ef0e978c4f768ae5c38338541edbf39e4e6742eb1a28c5ffcdb22 |
|
MD5 | 74ab4355125de335249c38c1b73c9840 |
|
BLAKE2b-256 | 50015600241d69e48cbf12160d0d53f900df4363f2ac7ab688ad21110bba26a7 |
Hashes for saxoncpe-12.0.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a78d3281c277a15e4accacac9b167fa1a6f4091f7c590b670a75e051e6060301 |
|
MD5 | 583d40f5c26d09ac6a8859051d053292 |
|
BLAKE2b-256 | 7cce131f0fc36e4446e79a7c8c027825407321244c7475632a1351b0d3f3f0b0 |
Hashes for saxoncpe-12.0.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4a8774b4f936a6c929119da35277afe53297e795bb19bd98a8cdcdee5cd4c3a |
|
MD5 | 3d730ab6495b634cfdbe2b4b43838fa1 |
|
BLAKE2b-256 | 66f62e1ce9c4270cc982f8219d5f66e74f0875e34e470728af30e62fee5bf7cf |
Hashes for saxoncpe-12.0.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad2240eda2648eaecf1f2d325802530774939eef0d2b18bd2db2f7898b474ac0 |
|
MD5 | d2b25b14113f8270b0f7f19f5108aa74 |
|
BLAKE2b-256 | 5511879f9c97c8ad55496cf21d1240184ee1b1e0585f51d14df2c6c14021d186 |
Hashes for saxoncpe-12.0.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c15ea978d3ab2acc320c83dff48de07fa251a03d0c8cd2c2826fe47b90aaad6e |
|
MD5 | 220e7787b6dc014cdc2969737921b9cb |
|
BLAKE2b-256 | 11cb41bb34ac4e208f1620f5864bfb63dbf3dc99ad214b8d5a807472661d4a07 |
Hashes for saxoncpe-12.0.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60862364a838328daf3d886fe30e1bca5bfc03950ceed7d4f326dd04c58f7bba |
|
MD5 | 601e6cb42ddb1b8974fc61871373e710 |
|
BLAKE2b-256 | a6b5124a590f4e8b9849f716e24886a2d782fc94b35d279622e2f77011b71cd5 |