Tree Diff and Embeddings

pygixml.diff compares two ordered trees (XML or HTML) and returns an edit script. pygixml.signature / pygixml.similarity embed the structure of a tree as a sparse vector. Everything runs in C++ (verbatim blocks inside src/embedding.pxi and src/diff.pxi, no extra dependency), one call per function, and the diff uses the embedding to decide which changed nodes belong together.

HTML needs no special support: parse it with pygixml.parse_html() and diff the documents. What counts as “the same” is a normalisation step (the idea behind html-differ), not a different algorithm.

import pygixml

a = pygixml.parse_html("<ul class='m'><li>Home<li>About us<li>Blog</ul>")
b = pygixml.parse_html("<ul class='m'><li>Home<li>About<li>Blog<li>Contact</ul>")

for action in pygixml.diff(a, b, text_diff=True):
    print(action)
# Insert(parent='/ul[1]', position=3, xml='<li>Contact</li>')
# UpdateText(path='/ul[1]/li[2]/text()[1]', text='About', old='About us',
#            diff=[(0, 'About'), (-1, ' us')])

pygixml.is_equal(a, b)                       # False
pygixml.similarity(a, b)                     # 0.889: structural similarity, 0..1

Edit script

diff(a, b) returns a list (empty when the trees are equivalent) of:

Action

Meaning

Insert(parent, position, xml)

add the node(s) in xml as child number position of parent. A wholly new subtree is one action

InsertText(parent, position, text)

add a text node

Delete(path)

remove a node with everything under it

Move(path, parent, position)

detach the node, then insert it into parent (evaluated after the detach) at position

UpdateText(path, text, old, diff)

replace the text of a text/comment node. diff holds (-1 | 0 | 1, piece) tuples when text_diff=True

SetAttrib(path, name, value, old)

set an attribute (old is None when it is new)

DeleteAttrib(path, name, old)

remove an attribute

Actions come in the order they must be applied (the new tree is walked breadth-first, so inserts near the top come before edits deeper down). Paths look like /ul[1]/li[2]/text()[1]: the index counts siblings of the same tag (or text() / comment()), and a path refers to the tree as it is when that action runs, after the earlier ones. They address the normalised view (blank text and comments are not there by default).

The script is checked before it is returned: applying it to the first tree must give the second, otherwise pygixml.PygiXMLDiffError is raised (that would be a bug). The tests also apply scripts with an independent reference patcher on 15,000 random tree pairs.

Applying a script

patch(doc, actions) applies a script to a document in place:

actions = pygixml.diff(a, b)
pygixml.patch(a, actions)
pygixml.is_equal(a, b)          # True
pygixml.diff(a, b)              # []

It reads paths through the same normalised view as diff, so what normalisation ignores (blank text, comments by default) is invisible to it and is left where it is. Pass the same ignore_whitespace / ignore_comments / class_as_set you gave diff. Every action checks what it expects (the path exists, the old text, the old attribute value), so a script is refused with pygixml.PygiXMLDiffError when the document is not the one it was made for. The document may then be partly patched; patch a copy if you need all-or-nothing. patch works on a document, not on a single element. Adjacent text and CDATA nodes (also when only an ignored comment or processing instruction separates them) are one text node in the normalised view, so patch first merges such runs in the document; the ignored comment itself stays, after the merged text.

Normalisation

option

default

effect

ignore_whitespace

True

drop blank text, trim and collapse runs

ignore_comments

True

comments are not compared

ignore_attrs

()

attribute names to ignore; "data-*" is a prefix; a single string is fine

class_as_set

True

class="b a a" equals class="a b"

attribute order

always

never matters

is_equal(a, b, **options) answers “did anything change?” in one pass by comparing subtree hashes, so it is the cheap check before a diff.

How the diff works

Two phases, as in Chawathe et al., Change Detection in Hierarchically Structured Information (1996):

  1. Matching, top-down, for every matched pair of parents. Children are paired by (a) keys (an id attribute: the same key means the same element wherever it sits); (b) identical subtree hash, with an LCS over the hash sequences; (c) what is left between those anchors, by similarity: pq-gram profiles for elements, character bigrams for text. A node that is the only one of its tag in its gap is treated as an update. With moves=True unmatched subtrees are also paired across parents, identical ones first, then ones at least move_threshold similar.

  2. Edit script: breadth-first over the new tree, mutating a working copy of the old one (insert, update, move), aligning each node’s children with a longest-increasing-subsequence, then deleting what is left.

The choice of per-level LCS over a global optimal matching follows Cobena, Abdessalem and Hinnach’s comparative study of XML change-detection tools: the LCS approach ran in linear time and stayed close to the minimal edit cost, while optimal algorithms (Zhang-Shasha, APTED/RTED) are quadratic or worse.

Limits (also marked ponytail: in src/diff.pxi):

  • the LCS is an exact table below a size cap and a greedy monotone match above it. Upgrade path: Myers / Wu O(ND);

  • elements are never renamed: a different tag is a delete plus an insert;

  • a subtree that is both moved to another parent and edited is only found when it stays similar enough (move_threshold);

  • the result is a good script, not a proven minimal one. Treat the exact output as unstable between versions.

Text diff

text_diff=True attaches a word-level diff to every UpdateText as a list of (-1 | 0 | 1, piece) tuples (delete / keep / insert). Texts are split into words, runs of whitespace, single punctuation marks and single CJK characters, then compared with the same LCS the tree matching uses, so brave -> new is one replacement. It also works for scripts such as Persian. ponytail: a character-level Myers diff with semantic clean-up is the upgrade path.

Embeddings

signature(node, method="pqgram", p=2, q=3, content=False) returns a sparse vector {hash: count}; similarity(a, b, ...) is the Dice coefficient of two such bags, 2|A n B| / (|A| + |B|), computed in one C++ call.

  • "pqgram" (Augsten, Bohlen, Gamper, The pq-Gram Distance between Ordered Labeled Trees, TODS 2010): every node anchors windows made of p ancestors and q consecutive children. The distance approximates the tree edit distance and lower-bounds the fanout-weighted one.

  • "paths": the bag of root-to-node tag paths. Cruder, cheaper, good for telling page templates apart.

Labels are tag names and text is one constant label, so the default compares structure only: two pages built from the same template are 1.0 similar whatever they say. content=True includes text. Both are linear in the size of the tree, so similarity makes a cheap pre-filter in front of diff.

Speed

Measured on one Linux machine, single thread; treat the numbers as indicative.

  • Catalog of 1,600 items (263 KB, 6,400 elements), 2 % of items edited: pygixml 15 ms and 32 actions for 32 edits; xmldiff 3.0 took 5.8 s (2.2 s with uniqueattrs=["id"]) and emitted 916 (498) operations.

  • A 487 KB real HTML page with 2 subtrees removed and 3 attributes added: pygixml 67 ms, 5 actions; xmldiff 3.0 with default settings 13.6 s, 816 operations. xmldiff was not tuned for this input.

  • 100,000 flat items (16 MB), 2 % edited: about 2.2 s.

Errors

Everything raises pygixml.PygiXMLDiffError, a subclass of pygixml.PygiXMLError: something that is not a node or document, a null node, an invalid option, a script that does not apply to the document, or an internal consistency failure.

API

pygixml.diff(a, b, *, ignore_whitespace=True, ignore_comments=True, ignore_attrs=(), class_as_set=True, keys=('id',), moves=True, text_diff=False, threshold=0.5, move_threshold=0.8, move_min_size=2, p=2, q=3)

Edit script that turns tree a into tree b (ordered trees).

Returns a list of Insert, InsertText, Delete, Move, UpdateText, SetAttrib and DeleteAttrib; empty when the trees are equivalent. The script is verified inside the call: applying it to a yields exactly b (as normalised below), otherwise PygiXMLDiffError is raised.

How nodes are paired, per matched pair of parents: elements with the same key attribute; subtrees with identical hash (an LCS over the hash sequences); then whatever is left between those anchors, by pq-gram similarity for elements and character-bigram similarity for text. With moves (default), unmatched subtrees are also paired across parents, identical ones first and then ones at least move_threshold similar.

Normalisation (what counts as “the same”, the idea behind html-differ):

Parameters:
  • a -- an XMLNode or XMLDocument. For HTML, pass what parse_html() returns.

  • b -- an XMLNode or XMLDocument. For HTML, pass what parse_html() returns.

  • ignore_whitespace -- drop blank text; trim and collapse runs of whitespace elsewhere.

  • ignore_comments -- leave comments out of the comparison.

  • ignore_attrs -- attribute names to ignore; a trailing * is a prefix ("data-*"). May be a single string.

  • class_as_set -- compare class as a set of names (order and repeats don’t matter). Attribute order never matters.

  • keys -- attributes that identify an element (("id",) by default); elements with the same key are paired wherever they are.

  • moves -- detect subtrees moved to another parent.

  • text_diff -- put a word-level diff in UpdateText.diff.

  • threshold -- minimum similarity (0..1) to pair two changed siblings that are not the only ones of their tag in the gap.

  • move_threshold -- minimum similarity to pair a moved-and-edited subtree.

  • move_min_size -- smallest subtree (node count) considered for moves.

  • p -- pq-gram shape used for the similarity.

  • q -- pq-gram shape used for the similarity.

Raises:

PygiXMLDiffError -- not a node/document, null node, invalid option, or an internal consistency failure.

pygixml.patch(doc, actions, *, ignore_whitespace=True, ignore_comments=True, class_as_set=True)

Apply an edit script from diff() to a document, in place.

After patch(a, diff(a, b)), a is equivalent to b (is_equal is true and diff(a, b) is empty). Paths are read through the same normalised view as diff(): blank text and (by default) comments are invisible, so they are left where they are. Use the same normalisation options as for diff().

Every action checks what it expects: the old text, the old attribute value, that the path exists. If the document is not the one the script was made for, PygiXMLDiffError is raised. The document may then be partly patched; patch a copy if you need all-or-nothing.

Parameters:
  • doc -- an XMLDocument.

  • actions -- the list returned by diff() (any iterable of actions).

  • ignore_whitespace -- as in diff().

  • ignore_comments -- as in diff().

  • class_as_set -- as in diff().

Raises:

PygiXMLDiffError -- not a document, not an action, or the script does not apply to this document.

pygixml.is_equal(a, b, *, ignore_whitespace=True, ignore_comments=True, ignore_attrs=(), class_as_set=True)

Are two trees equivalent after normalisation? (same options as diff()). One C++ call, linear time: it compares subtree hashes, so it is the cheap way to ask “did anything change?” before computing a diff.

Raises:

PygiXMLDiffError -- not a node/document, or a null node.

pygixml.signature(node, method='pqgram', p=2, q=3, content=False, *, ignore_whitespace=True, ignore_comments=True, ignore_attrs=(), class_as_set=True)

Embed the structure of a tree as a sparse vector: {hash: count}.

Two methods, both linear in the size of the tree:

  • "pqgram" (default): pq-grams (Augsten, Bohlen, Gamper, TODS 2010). Each node anchors windows made of p ancestors and q consecutive children; their distance approximates the tree edit distance and lower-bounds the fanout-weighted one.

  • "paths": the bag of root-to-node tag paths. Cruder and cheaper; good for telling page templates apart.

Labels are tag names; text counts as one constant label, so the default is structure only. Pass content=True to include text and comment content.

The vector is hash-keyed, so it compares with similarity() (or your own dot product), not by key meaning.

Parameters:
  • node -- an XMLNode or XMLDocument.

  • method -- "pqgram" or "paths".

  • p -- pq-gram shape (stem length, base width).

  • q -- pq-gram shape (stem length, base width).

  • content -- include text/comment content in the labels.

  • ignore_whitespace -- normalisation, same meaning as in diff().

  • ignore_comments -- normalisation, same meaning as in diff().

  • ignore_attrs -- normalisation, same meaning as in diff().

  • class_as_set -- normalisation, same meaning as in diff().

Raises:

PygiXMLDiffError -- not a node/document, null node, or invalid option.

pygixml.similarity(a, b, method='pqgram', p=2, q=3, content=False, *, ignore_whitespace=True, ignore_comments=True, ignore_attrs=(), class_as_set=True)

Structural similarity of two trees in [0, 1]: the Dice coefficient of their signature() bags (1 = same structure, 0 = nothing shared).

Computed in a single C++ call, without building the two dicts. Runs in time linear in the size of the trees, so it is a cheap pre-filter before an expensive diff().

Raises:

PygiXMLDiffError -- see signature().

exception pygixml.PygiXMLDiffError

Raised by diff(), is_equal(), signature() and similarity(): something that isn’t a node or document, a null node, an invalid option, or (a bug if it ever happens) an edit script that does not reproduce the target tree. Subclass of PygiXMLError.