Skip to main content

Convert between XML trees and span representation

Project description

Spans and Trees

A small Python library for converting between XML trees and a span-based structure. This can be useful for extracting sections of text from XML documents and doing special things with some of the tags.

The two main functions are treeToSpans and spansToTrees for converting between an ElementTree element and text with a list of spans. Examples are shown below.

treeToSpans

First create a little example XML tree to convert.

import xml.etree.ElementTree as ET

xmlstring = "<doc><title>Important document</title><contents>Empty</contents></doc>"
root = ET.ElementTree(ET.fromstring(xmlstring)).getroot()

Then use the treeToSpans function to convert the XML document into the text content with spans.

from spans_and_trees import treeToSpans

text, spans = treeToSpans(root)

print(text)  # Important documentEmpty
print(spans) # [(0, 18, 'title', {}), (18, 5, 'contents', {})]

The format of the spans are a tuple of length 4. The element contents are:

  1. The start location of the span
  2. The length of the span
  3. The tag of the span
  4. A dictionary of the attributes of the span.

spansToTrees

Now we create a dummy document with a block of text and a span at particular offset.

from spans_and_trees import spansToTree

text = 'The quick brown fox jumped over the lazy dog'
spans = [ (10,5,'colour',{'dummy_attrib':'5'}) ] # The span starts at 10, has length of 5, is a 'colour' tag and has a dummy attribute.

root = spansToTree(text, spans)

print(type(root)) # <class 'xml.etree.ElementTree.Element'>

We can check the XML tree that has been created:

xmlstr = ET.tostring(root)

print(xmlstr) # b'<anon>The quick <colour dummy_attrib="5">brown</colour> fox jumped over the lazy dog</anon>'

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

spans_and_trees-0.1.2.tar.gz (5.3 kB view details)

Uploaded Source

File details

Details for the file spans_and_trees-0.1.2.tar.gz.

File metadata

  • Download URL: spans_and_trees-0.1.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for spans_and_trees-0.1.2.tar.gz
Algorithm Hash digest
SHA256 506ad7ab4deae6e56924e924fa00b0d9aecf5a0ee0edffe8bec76e95ee1ebaaf
MD5 8e80e9492fab1a5b4c567134c1cdf260
BLAKE2b-256 a7d4587116a3ddc80b5d8a3730902121763f7a2133dd79e6b2fb58a095f6ee0a

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