Skip to main content

Python bindings for the Uppsala XML library

Project description

pyuppsala

Python bindings for the Uppsala XML library -- a zero-dependency, pure-Rust implementation of XML 1.0, Namespaces, XPath 1.0, and XSD validation.

pyuppsala gives you a fast, correct, and memory-safe XML toolkit from Python with no C dependencies to compile and no transitive native libraries to audit.

This release is against 0.7.1 of Uppsala library.

Features

  • XML 1.0 parsing with full well-formedness checking
  • Namespace-aware DOM with tree mutation (create, append, insert, remove, detach)
  • XPath 1.0 evaluation (all axes, functions, predicates)
  • XSD validation (structures + datatypes, 40+ built-in types, facets, complex types)
  • XSD regex pattern matching (Unicode categories, blocks, character class subtraction)
  • Imperative XML builder (XmlWriter) for constructing output without a DOM
  • Serialization with pretty-printing, compact output, and streaming to files
  • Automatic encoding detection for UTF-8 and UTF-16 (LE/BE)
  • lxml.etree-compatible API via pyuppsala.etree, a near drop-in for much of lxml.etree backed by Uppsala's secure parser

Read the full documentation

Installation

python3 -m pip install pyuppsala

Or with uv:

uv add pyuppsala

Wheels are compiled from Rust via maturin. Python 3.10+ is required.

Quick start

Parse and query

from pyuppsala import Document, XPathEvaluator

doc = Document("<bookstore><book><title>Moby Dick</title></book></bookstore>")
doc.prepare_xpath()

xpath = XPathEvaluator()
title = xpath.evaluate(doc, "string(//title)")
print(title)  # "Moby Dick"

Build XML

from pyuppsala import XmlWriter

w = XmlWriter()
w.write_declaration()
w.start_element("catalog", [("xmlns", "urn:example")])
w.start_element("item", [("id", "1")])
w.text("Widget")
w.end_element("item")
w.end_element("catalog")
print(w.to_string())

Validate against an XSD schema

from pyuppsala import XsdValidator

schema = """\
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="greeting" type="xs:string"/>
</xs:schema>
"""

validator = XsdValidator(schema)
print(validator.is_valid_str("<greeting>Hello!</greeting>"))  # True
print(validator.is_valid_str("<greeting><bad/></greeting>"))  # False

Mutate the DOM

from pyuppsala import Document

doc = Document("<root><a/></root>")
root = doc.document_element
b = doc.create_element("b")
doc.append_child(root, b)
print(doc.to_xml())  # <root><a/><b/></root>

XSD regex

from pyuppsala import XsdRegex

regex = XsdRegex(r"[0-9]{5}")
print(regex.is_match("12345"))  # True
print(regex.is_match("abcde"))  # False

lxml-compatible etree API

Code written for lxml.etree runs after swapping the import. Elements are live views over the underlying document, with stable identity and the familiar .text/.tail/.attrib model.

from pyuppsala import etree  # instead of: from lxml import etree

root = etree.fromstring("<catalog><book id='1'>Dune</book></catalog>")
print(root.find("book").text)        # Dune
print(root[0].get("id"))             # 1

cat = etree.Element("catalog")
book = etree.SubElement(cat, "book", {"id": "2"})
book.text = "Neuromancer"
print(etree.tostring(cat, encoding="unicode"))
# <catalog><book id="2">Neuromancer</book></catalog>

See the etree documentation for the supported and unsupported feature matrix.

API overview

Class / function Purpose
Document(xml) Parse XML string into a DOM
Document.from_bytes(data) Parse XML bytes (auto-detects UTF-8/UTF-16)
Document.empty() Create an empty document for building from scratch
Node A handle to a node in the document tree
QName A qualified XML name (local name + optional namespace + prefix)
Attribute An XML attribute (name + value)
XPathEvaluator Evaluate XPath 1.0 expressions
XsdValidator(schema) Validate documents against an XSD schema
XmlWriter Imperative XML builder (no DOM needed)
XsdRegex(pattern) XSD regular expression pattern matcher
parse(xml) Module-level shorthand for Document(xml)
parse_bytes(data) Module-level shorthand for Document.from_bytes(data)
pyuppsala.etree lxml.etree-compatible API (Element, SubElement, fromstring, tostring, find/findall, XPath, XMLSchema, ...)

Exceptions

Exception Raised when
XmlParseError XML is syntactically malformed
XmlWellFormednessError XML violates well-formedness constraints
XmlNamespaceError Namespace prefix is undeclared or misused
XPathError XPath expression is invalid
XsdValidationError XSD schema itself is invalid

All exceptions inherit from Exception.

Type stubs

Type stubs (pyuppsala/__init__.pyi and pyuppsala/etree.pyi, marked with py.typed) ship with the package for full IDE auto-completion and type-checking with mypy/pyright.

Development

# Clone the repository
git clone https://github.com/kushaldas/pyuppsala.git
cd pyuppsala

# Set up the environment with uv
uv sync

# Build the native extension in development mode
uv run maturin develop

# Run the test suite
uv run pytest

# Build a release wheel
uv run maturin build --release

License

BSD-2-Clause

Project details


Download files

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

Source Distribution

pyuppsala-0.7.1.tar.gz (165.4 kB view details)

Uploaded Source

Built Distributions

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

pyuppsala-0.7.1-cp310-abi3-win_arm64.whl (780.3 kB view details)

Uploaded CPython 3.10+Windows ARM64

pyuppsala-0.7.1-cp310-abi3-win_amd64.whl (821.5 kB view details)

Uploaded CPython 3.10+Windows x86-64

pyuppsala-0.7.1-cp310-abi3-manylinux_2_28_x86_64.whl (857.5 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ x86-64

pyuppsala-0.7.1-cp310-abi3-macosx_11_0_arm64.whl (791.2 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file pyuppsala-0.7.1.tar.gz.

File metadata

  • Download URL: pyuppsala-0.7.1.tar.gz
  • Upload date:
  • Size: 165.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyuppsala-0.7.1.tar.gz
Algorithm Hash digest
SHA256 8fee0cec3a4e26868eb9c3dc6313b1b68fa16599c4ec48ae83e9601c2af6143b
MD5 674618534b9c4feaa168d4290930277b
BLAKE2b-256 887d0a14cfd3d0770faaa7d78bb09c55701aedcdc9d2b2844500cfb3a3fef085

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyuppsala-0.7.1.tar.gz:

Publisher: release.yml on kushaldas/pyuppsala

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyuppsala-0.7.1-cp310-abi3-win_arm64.whl.

File metadata

  • Download URL: pyuppsala-0.7.1-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 780.3 kB
  • Tags: CPython 3.10+, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyuppsala-0.7.1-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 eda7a25eebc2ab12b6ed156acdbefc3483f7a9b6ce3d5cb98706835bad47f875
MD5 9ded526ea01c7c75a0fb9f4bb9f8e48d
BLAKE2b-256 c4af0548d7b16f41cd35e6a100e41e65caade857095e0a9bc6807817bd1cafa4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyuppsala-0.7.1-cp310-abi3-win_arm64.whl:

Publisher: release.yml on kushaldas/pyuppsala

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyuppsala-0.7.1-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: pyuppsala-0.7.1-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 821.5 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyuppsala-0.7.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 987ed06e84d20c4f42b37d10a6f1b1486998268e1486b6e3a22e24d6a8db3e9c
MD5 bf64b6b25e9bec3464bfee45d51c4c94
BLAKE2b-256 466ca00a0aedd979638d1b27df71674739e8dd2c45604f7c5dfff6cba5a73b96

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyuppsala-0.7.1-cp310-abi3-win_amd64.whl:

Publisher: release.yml on kushaldas/pyuppsala

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyuppsala-0.7.1-cp310-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyuppsala-0.7.1-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f7adf04c79ef1a9b6333b4b94721d60877ae7b546bdd34d69139d558b72328b9
MD5 c710f7c686fd86811d33aa37f805bf34
BLAKE2b-256 fb9c48432e76d6f17312059f9cf4956f107fb28e56c5c099b4e89d9218a8f996

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyuppsala-0.7.1-cp310-abi3-manylinux_2_28_x86_64.whl:

Publisher: release.yml on kushaldas/pyuppsala

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyuppsala-0.7.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyuppsala-0.7.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cb63ccf3b90472bd7fdef2d3d64f05f04fb3aea1f8effbd2d12ad78064f0d40a
MD5 b2abcafc1b20ce631232537f58729c97
BLAKE2b-256 8f94fd3a23315e830e20d2506ba002710f32b2a1c01f34fbcff8052d432f8f08

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyuppsala-0.7.1-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on kushaldas/pyuppsala

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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