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.

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)

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

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)

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

A pyuppsala.pyi file is included 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.4.0.tar.gz (74.9 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.4.0-cp310-abi3-win_arm64.whl (591.6 kB view details)

Uploaded CPython 3.10+Windows ARM64

pyuppsala-0.4.0-cp310-abi3-win_amd64.whl (615.7 kB view details)

Uploaded CPython 3.10+Windows x86-64

pyuppsala-0.4.0-cp310-abi3-manylinux_2_28_x86_64.whl (693.2 kB view details)

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

pyuppsala-0.4.0-cp310-abi3-macosx_11_0_arm64.whl (636.3 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for pyuppsala-0.4.0.tar.gz
Algorithm Hash digest
SHA256 658db520483cbc605f0060ab3338cbf828971b4ee410279a57b9112549d528a0
MD5 3a0c80d5c3acc1d1c2412ef9009c96fe
BLAKE2b-256 959edc6d95c82d781e84822bf1c9bf28b17c9ff0e2cf4e392da5347c237cdd38

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyuppsala-0.4.0.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.4.0-cp310-abi3-win_arm64.whl.

File metadata

  • Download URL: pyuppsala-0.4.0-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 591.6 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.4.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 ca955544baf6effeecd72cb9b4bced2baebd409037d99dac33d9eba462917c15
MD5 06f20a8b3ad447d1671d631d33ba9f1d
BLAKE2b-256 79925caad57dd3548e9f0461dcc6884a6b0923488a854a9649feb747eb8463d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyuppsala-0.4.0-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.4.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: pyuppsala-0.4.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 615.7 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.4.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 87ddf8195018b87ca867456adaf5b5c28648f96a4c81231d861d362a8cb134a4
MD5 11fceb06ff517356664557bc28bde92e
BLAKE2b-256 aa16940b5516c5608fba13bf588d6e7af59adcf396773c85137a65527f39c8dd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyuppsala-0.4.0-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.4.0-cp310-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyuppsala-0.4.0-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d372a80c7f1be297ab4850e698e5c3a459e0a4acb518b85860abe1317ca6d814
MD5 1ba368c1a4a2b4dc48bf8c7faa0bd9e5
BLAKE2b-256 5539eb3246fce25ca9d6674f1c4a21e174a9c66d1ca56043b0e861e9c9d34122

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyuppsala-0.4.0-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.4.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyuppsala-0.4.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ed97e1454b8fcc3f6275c14aba188a8ca2a946081eb3a797be985c715c07473e
MD5 ec65371251958772b5d843880206be98
BLAKE2b-256 3c9e7761e21b47f769617f2a27d270630cd2163b485b3391435dd20309491060

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyuppsala-0.4.0-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