Python API ========== Main diffing API ---------------- Using ``xmldiff`` from Python is very easy, you just import and call one of the three main API methods. .. doctest:: :options: -ELLIPSIS, +NORMALIZE_WHITESPACE >>> from xmldiff import main >>> main.diff_files("../tests/test_data/insert-node.left.html", ... "../tests/test_data/insert-node.right.html", ... diff_options={'F': 0.5, 'ratio_mode': 'fast'}) [UpdateTextIn(node='/body/div[1]', text=None), InsertNode(target='/body/div[1]', tag='p', position=0), UpdateTextIn(node='/body/div/p[1]', text='Simple text')] Which one you choose depends on if the XML is contained in files, text strings or ``lxml`` trees. * ``xmldiff.main.diff_files()`` takes as input paths to files, or file streams. * ``xmldiff.main.diff_texts()`` takes as input Unicode strings. * ``xmldiff.main.diff_trees()`` takes as input lxml trees. The arguments to these functions are the same: Parameters .......... ``left``: The "left", "old" or "from" XML. The diff will show the changes to transform this XML to the "right" XML. ``right``: The "right", "new" or "target" XML. ``check``: Return error code 1 if there are any differences between the files. ``diff_options``: A dictionary containing options that will be passed into the ``Differ()``: ``F``: A value between 0 and 1 that determines how similar two XML nodes must be to match as the same in both trees. Defaults to ``0.5``. A higher value requires a smaller difference between two nodes for them to match. Set the value high, and you will see more nodes inserted and deleted instead of being updated. Set the value low, and you will get more updates instead of inserts and deletes. ``uniqueattrs``: A list of XML node attributes that will uniquely identify a node. See `Unique Attributes`_ for more info. Defaults to ``['{http://www.w3.org/XML/1998/namespace}id']``. ``ratio_mode``: The ``ratio_mode`` determines how accurately the similarity between two nodes is calculated. The choices are ``'accurate'``, ``'fast'`` and ``'faster'``. Defaults to ``'fast'``. Using ``'faster'`` often results in less optimal edits scripts, in other words, you will have more actions to achieve the same result. Using ``'accurate'`` will be significantly slower, especially if your nodes have long texts or many attributes. ``ignored_attrs``: A list of XML node attributes that will be ignored in comparison. ``fast_match``: By default ``xmldiff`` will compare each node from one tree with all nodes from the other tree. It will then pick the one node that matches best as the match, if that match passes the match threshold ``F`` (see above). If fast_match is true ``xmldiff`` will first make a faster run, trying to find chains of matching nodes, during which any match better than ``F`` will count. This significantly cuts down on the time to match nodes, but means that the matches are no longer the best match, only "good enough" matches. ``formatter``: The formatter to use, see `Using Formatters`_. If no formatter is specified the function will return a list of edit actions, see `The Edit Script`_. Result ...... If no formatter is specified the diff functions will return a list of actions. Such a list is called an Edit Script and contains all changes needed to transform the "left" XML into the "right" XML. If a formatter is specified that formatter determines the result. The included formatters, ``diff``, ``xml``, and ``old`` all return a Unicode string. ``xmldiff`` is still under rapid development, and no guarantees are done that the output of one version will be the same as the output of any previous version. The actions of the edit script can be in a different order or replaced by equivalent actions dependingon the version of ``xmldiff``, but if the Edit Script does not correctly transform one XML tree into another, that is regarded as a bug. This means that the output of the ``xml`` format also may change from version to version. There is no "correct" solution to how that output should look, as the same change can be represented in several different ways. Unique Attributes ----------------- The ``uniqueattrs`` argument is a list of strings or ``(tag, attribute)`` tuples specifying attributes that uniquely identify a node in the document. This is used by the differ when trying to match nodes. If one node in the left tree has a this attribute, the node in the right three with the same value for that attribute will match, regardless of other attributes, child nodes or text content. Respectively, if the values of the attribute on the nodes in question are different, or if only one of the nodes has this attribute, the nodes will not match regardless of their structural similarity. In case the attribute is a tuple, the attribute match applies only if both nodes have the given tag. The default is ``['{http://www.w3.org/XML/1998/namespace}id']``, which is the ``xml:id`` attribute. But if your document have other unique identifiers, you can pass them in instead. If you for some reason do not want the differ to look at the ``xml:id`` attribute, pass in an empty list. Using Formatters ---------------- By default the diff functions will return an edit script, but if you pass in a formatter the result will be whatever that formatter returns. The three included formatters all return Unicode strings. All formatters take two arguments: :``normalize``: This argument determines whitespace normalizing. It can be one of the following values, all defined in ``xmldiff.formatting``: :``WS_NONE``: No normalizing :``WS_TAGS``: Normalize whitespace between tags :``WS_TEXT``: Normalize whitespace in text tags (only used by the ``XMLFormatter``). :``WS_BOTH``: Both ``WS_TAGS`` and ``WS_TEXT``. :``pretty_print``: This argument determines if the output should be compact (``False``) or readable (``True``). Only the ``XMLFormatter`` currently uses this parameter, but it's useful enough that it was included in the ``BaseFormatter`` class, so that all subsequent formatters may use it. DiffFormatter ............. .. py:class:: xmldiff.formatting.DiffFormatter(normalize=WS_TAGS, pretty_print=False) This formatter is the one used when you specify ``-f diff`` on the command line. It will return a string with the edit script printed out, one action per line. Each line is enclosed in brackets and consists of a string describing the action, and the actions arguments. This is the output format of xmldiff 0.6/1.x, however, the actions and arguments are not the same, so the output is not compatible. .. doctest:: :options: -ELLIPSIS, +NORMALIZE_WHITESPACE >>> from xmldiff import formatting >>> formatter = formatting.DiffFormatter() >>> print(main.diff_files("../tests/test_data/insert-node.left.html", ... "../tests/test_data/insert-node.right.html", ... formatter=formatter)) [update-text, /body/div[1], null] [insert, /body/div[1], p, 0] [update-text, /body/div/p[1], "Simple text"] XmlDiffFormatter ................ .. py:class:: xmldiff.formatting.XmlDiffFormatter(normalize=WS_TAGS, pretty_print=False) This formatter works like the DiffFormatter, but the output format is different and more similar to the ``xmldiff`` output in versions 0.x and 1.x. .. doctest:: :options: -ELLIPSIS, +NORMALIZE_WHITESPACE >>> from xmldiff import formatting >>> formatter = formatting.XmlDiffFormatter(normalize=formatting.WS_NONE) >>> print(main.diff_files("../tests/test_data/insert-node.left.html", ... "../tests/test_data/insert-node.right.html", ... formatter=formatter)) [update, /body/div[1]/text()[1], "\n "] [insert-first, /body/div[1],
] [update, /body/div/p[1]/text()[1], "Simple text"] [update, /body/div/p[1]/text()[2], "\n "] XMLFormatter ............ .. py:class:: xmldiff.formatting.XMLFormatter(normalize=WS_NONE, pretty_print=True, text_tags=(), formatting_tags=())ΒΆ :param text_tags: A list of XML tags that contain human readable text, ex ``('para', 'li')`` :param formatting_tags: A list of XML tags that are tags that change text formatting, ex ``('strong', 'i', 'u' )`` This formatter return XML with tags describing the changes. These tags are designed so they easily can be changed into something that will render nicely, for example with XSLT replacing the tags with the format you need. .. doctest:: :options: -ELLIPSIS, +NORMALIZE_WHITESPACE >>> from xmldiff import formatting >>> formatter = formatting.XMLFormatter(normalize=formatting.WS_BOTH) >>> print(main.diff_files("../tests/test_data/insert-node.left.html", ... "../tests/test_data/insert-node.right.html", ... formatter=formatter))Simple text
Simple text