Advanced Usage ============== Diffing Formatted Text ---------------------- You can write your own formatter that understands your XML format, and therefore can apply some intelligence to the format. One common use case for this is to have more intelligent text handling. The standard formatters will treat any text as just a value, and the resulting diff will simply replace one value with another: .. doctest:: :options: -ELLIPSIS, +NORMALIZE_WHITESPACE >>> from xmldiff import main, formatting >>> left = '
Old Content
' >>> right = 'New Content
' >>> main.diff_texts(left, right) [UpdateTextIn(node='/body/p[1]', text='New Content')] The ``xml`` formatter will set tags around the text marking it as inserted or deleted: .. doctest:: :options: -ELLIPSIS, +NORMALIZE_WHITESPACE >>> formatter=formatting.XMLFormatter() >>> >>> left = 'Old Content
' >>> right = 'New Content
' >>> result = main.diff_texts(left, right, formatter=formatter) >>> print(result)My Fine Content
' >>> right = 'My Fine Content
' >>> result = main.diff_texts(left, right, formatter=formatter) >>> print(result)
My Fine
My Fine Content
My Fine Content
This gives a result that flags the ```` tag as new formatting. This more compact output is much more useful and easier to transform into a visual output. Making a Visual Diff -------------------- XML and HTML views will of course ignore all these ``diff:`` tags and attributes. What we want with the HTML output above is to transform the ``diff:insert-formatting`` attribute into something that will make the change visible. We can achieve that by applying XSLT before the ``render()`` method in the formatter. This requires subclassing the formatter: .. doctest:: :options: -ELLIPSIS, +NORMALIZE_WHITESPACE >>> import lxml.etree >>> XSLT = u''' ...My Fine Content
You can then add into your CSS files classes that make inserted text green, deleted text red with an overstrike, and formatting changes could for example be blue. This makes it easy to see what has been changed in a HTML document. Performance Options ------------------- The performance options available will not just change the performance, but can also change the result. The result will not necessarily be worse, it will just be less accurate. In some cases the less accurate result might actually be preferrable. As an example we take the following HTML codes: .. doctest:: :options: -ELLIPSIS, +NORMALIZE_WHITESPACE >>> left = u""" ...The First paragraph
...A Second paragraph
...Last paragraph
... """ >>> right = u""" ...Last paragraph
...A Second paragraph
...The First paragraph
... """ >>> result = main.diff_texts(left, right) >>> result [MoveNode(node='/html/body/p[1]', target='/html/body[1]', position=2), MoveNode(node='/html/body/p[1]', target='/html/body[1]', position=1)] We here see that the differ finds that two paragraphs needs to be moved. Don't be confused that it says ``p[1]`` in both cases. That just means to move the first paragraph, and in the second case that first paragraph has already been moved and is now last. If we format that diff to XML with the XMLFormatter, we get output that marks these paragraphs as deleted and then inserted later. .. doctest:: :options: -ELLIPSIS, +NORMALIZE_WHITESPACE >>> formatter = HTMLFormatter( ... normalize=formatting.WS_BOTH) >>> result = main.diff_texts(left, right, formatter=formatter) >>> print(result)The First paragraph
A Second paragraph
Last paragraph
A Second paragraph
The First paragraph
Let's try diffing the same HTML with the fast match algorithm: .. doctest:: :options: -ELLIPSIS, +NORMALIZE_WHITESPACE >>> result = main.diff_texts(left, right, ... diff_options={'fast_match': True}) >>> result [UpdateTextIn(node='/html/body/p[1]', text='Last paragraph'), UpdateTextIn(node='/html/body/p[3]', text='The First paragraph')] Now we instead got two update actions. This means the resulting HTML is quite different: .. doctest:: :options: -ELLIPSIS, +NORMALIZE_WHITESPACE >>> result = main.diff_texts(left, right, ... diff_options={'fast_match': True}, ... formatter=formatter) >>> print(result)The FirLast paragraph
A Second paragraph
LaThe First paragraph