yaxmldiff


Nameyaxmldiff JSON
Version 0.2.0 PyPI version JSON
download
home_pageNone
Summaryyaxmldiff is Yet Another XML Differ
upload_time2024-09-29 14:53:20
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseApache-2.0
keywords diff xml
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # yaxmldiff – Yet Another XML Diff Library

This library checks if two XML documents seem semantically equivalent.
If not, it produces something similar to a unified diff.

Example:

```pycon
>>> from yaxmldiff import compare_xml
>>> print(compare_xml("<same/>", "  <same /> <!--ignored-->"))
None
>>> print(compare_xml("<doc><a id='a'/></doc>", "<doc><a name='a'/></doc>"))
  <doc>
    <a
-     id="a"
+     name="a"
    />
  </doc>

```

## `compare_xml()`

Compare two XML documents.

If the documents are given as strings, they are parsed first.
Alternatively, the documents can be given as an `lxml.etree` object.

Returns: None if both are equal, a diff otherwise.

Signature:

``` python
def compare_xml(
    left: str | Element,
    right: str | Element,
) -> str | None:
```

## Examples

Example: equal documents

```pycon
>>> print(compare_xml("<a/>", "<a/>"))
None

```

Example: different tag

```pycon
>>> print(compare_xml("<a/>", "<b x='2'/>"))
- <a/>
+ <b .../>

```

Example: changed text

```pycon
>>> print(compare_xml("<root><a/>foo</root>", "<root><a/>bar</root>"))
  <root>
    <a/>
-   foo
+   bar
  </root>

```

Example: nested changed text, collapses other nodes

```pycon
>>> print(compare_xml(
...     "<root><uninteresting a='b'>foo</uninteresting><scope>a</scope></root>",
...     "<root><uninteresting a='b'>foo</uninteresting><scope>b</scope></root>",
... ))
  <root>
    <uninteresting ...>...</uninteresting>
    <scope>
-     a
+     b
    </scope>
  </root>

```

Example: inserted node

```pycon
>>> print(compare_xml("<r><a/></r>", "<r><a/><b/></r>"))
  <r>
    <a/>
+   <b/>
  </r>

```

Example: changed attributes

```pycon
>>> print(compare_xml(
...     "<a onlya='1' both='2' changed='3'/>",
...     "<a onlyb='1' both='2' changed='4'/>",
... ))
  <a both="2"
-   onlya="1"
-   changed="3"
+   changed="4"
+   onlyb="1"
  />

```

Example: can hande encoding declarations

```pycon
>>> print(compare_xml(
...     "<?xml version='1.0' encoding='UTF-8'?><a/>",
...     "<a/>",
... ))
None

```

Example: comparison ignores surrounding space and newlines

```pycon
>>> print(compare_xml("<a>b<c/></a>", "\n <a> \n b \n <c \n/> \n </a> \n "))
None

```

Example: pre-parse documents

```pycon
>>> import lxml.etree
>>> print(compare_xml(lxml.etree.XML('<a parsed="yes"/>'), "<a parsed='no'/>"))
  <a
-   parsed="yes"
+   parsed="no"
  />

```

## Related software

There are tons of XML diffing tools for Python.

Most closely related is [`lxml.doctestcompare`](https://lxml.de/apidoc/lxml.doctestcompare.html).
The lxml variant has lots of useful tools for doctests,
such as ignoring subtrees with an `<any>` tag or content with an `...` ellipsis.
In contrast, yaxmldiff will compare two documents without further transformations.
Another big difference is in the output.
Whereas lxml will add inline annotations,
yaxmldiff tries to emulate a unified diff,
and will collapse uninteresting parts of the document.

## Contributing

Use [uv](https://docs.astral.sh/uv) for virtualenv management.
After installing uv, run `uv sync --all-extras --dev` to install dependencies.

Common development tasks are managed via the [`just` tasks runner](https://github.com/casey/just).
Install it via your package manager.
If in doubt, use `pipx install rust-just`.
Once installed, run `just` or `just qa` for a complete QA pipeline with linters+typechecking+tests.
Run `just -l` to get a list of all recipes.

## License

Copyright 2021-2024 Lukas Atkinson

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


# Changelog

## 0.2.0 – 2024-09-29

* minimum Python version is 3.8
* (internal) packaging modernization

## 0.1.0 - 2021-06-13

* initial release

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "yaxmldiff",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "diff, xml",
    "author": null,
    "author_email": "Lukas Atkinson <opensource@LukasAtkinson.de>",
    "download_url": "https://files.pythonhosted.org/packages/c3/1e/dbc8d5cfa845c6e5a125562ccdfdc1bb26d84891e15f4b6de082add76a3d/yaxmldiff-0.2.0.tar.gz",
    "platform": null,
    "description": "# yaxmldiff \u2013 Yet Another XML Diff Library\n\nThis library checks if two XML documents seem semantically equivalent.\nIf not, it produces something similar to a unified diff.\n\nExample:\n\n```pycon\n>>> from yaxmldiff import compare_xml\n>>> print(compare_xml(\"<same/>\", \"  <same /> <!--ignored-->\"))\nNone\n>>> print(compare_xml(\"<doc><a id='a'/></doc>\", \"<doc><a name='a'/></doc>\"))\n  <doc>\n    <a\n-     id=\"a\"\n+     name=\"a\"\n    />\n  </doc>\n\n```\n\n## `compare_xml()`\n\nCompare two XML documents.\n\nIf the documents are given as strings, they are parsed first.\nAlternatively, the documents can be given as an `lxml.etree` object.\n\nReturns: None if both are equal, a diff otherwise.\n\nSignature:\n\n``` python\ndef compare_xml(\n    left: str | Element,\n    right: str | Element,\n) -> str | None:\n```\n\n## Examples\n\nExample: equal documents\n\n```pycon\n>>> print(compare_xml(\"<a/>\", \"<a/>\"))\nNone\n\n```\n\nExample: different tag\n\n```pycon\n>>> print(compare_xml(\"<a/>\", \"<b x='2'/>\"))\n- <a/>\n+ <b .../>\n\n```\n\nExample: changed text\n\n```pycon\n>>> print(compare_xml(\"<root><a/>foo</root>\", \"<root><a/>bar</root>\"))\n  <root>\n    <a/>\n-   foo\n+   bar\n  </root>\n\n```\n\nExample: nested changed text, collapses other nodes\n\n```pycon\n>>> print(compare_xml(\n...     \"<root><uninteresting a='b'>foo</uninteresting><scope>a</scope></root>\",\n...     \"<root><uninteresting a='b'>foo</uninteresting><scope>b</scope></root>\",\n... ))\n  <root>\n    <uninteresting ...>...</uninteresting>\n    <scope>\n-     a\n+     b\n    </scope>\n  </root>\n\n```\n\nExample: inserted node\n\n```pycon\n>>> print(compare_xml(\"<r><a/></r>\", \"<r><a/><b/></r>\"))\n  <r>\n    <a/>\n+   <b/>\n  </r>\n\n```\n\nExample: changed attributes\n\n```pycon\n>>> print(compare_xml(\n...     \"<a onlya='1' both='2' changed='3'/>\",\n...     \"<a onlyb='1' both='2' changed='4'/>\",\n... ))\n  <a both=\"2\"\n-   onlya=\"1\"\n-   changed=\"3\"\n+   changed=\"4\"\n+   onlyb=\"1\"\n  />\n\n```\n\nExample: can hande encoding declarations\n\n```pycon\n>>> print(compare_xml(\n...     \"<?xml version='1.0' encoding='UTF-8'?><a/>\",\n...     \"<a/>\",\n... ))\nNone\n\n```\n\nExample: comparison ignores surrounding space and newlines\n\n```pycon\n>>> print(compare_xml(\"<a>b<c/></a>\", \"\\n <a> \\n b \\n <c \\n/> \\n </a> \\n \"))\nNone\n\n```\n\nExample: pre-parse documents\n\n```pycon\n>>> import lxml.etree\n>>> print(compare_xml(lxml.etree.XML('<a parsed=\"yes\"/>'), \"<a parsed='no'/>\"))\n  <a\n-   parsed=\"yes\"\n+   parsed=\"no\"\n  />\n\n```\n\n## Related software\n\nThere are tons of XML diffing tools for Python.\n\nMost closely related is [`lxml.doctestcompare`](https://lxml.de/apidoc/lxml.doctestcompare.html).\nThe lxml variant has lots of useful tools for doctests,\nsuch as ignoring subtrees with an `<any>` tag or content with an `...` ellipsis.\nIn contrast, yaxmldiff will compare two documents without further transformations.\nAnother big difference is in the output.\nWhereas lxml will add inline annotations,\nyaxmldiff tries to emulate a unified diff,\nand will collapse uninteresting parts of the document.\n\n## Contributing\n\nUse [uv](https://docs.astral.sh/uv) for virtualenv management.\nAfter installing uv, run `uv sync --all-extras --dev` to install dependencies.\n\nCommon development tasks are managed via the [`just` tasks runner](https://github.com/casey/just).\nInstall it via your package manager.\nIf in doubt, use `pipx install rust-just`.\nOnce installed, run `just` or `just qa` for a complete QA pipeline with linters+typechecking+tests.\nRun `just -l` to get a list of all recipes.\n\n## License\n\nCopyright 2021-2024 Lukas Atkinson\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n\n\n# Changelog\n\n## 0.2.0 \u2013 2024-09-29\n\n* minimum Python version is 3.8\n* (internal) packaging modernization\n\n## 0.1.0 - 2021-06-13\n\n* initial release\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "yaxmldiff is Yet Another XML Differ",
    "version": "0.2.0",
    "project_urls": {
        "GitHub": "https://github.com/latk/yaxmldiff.py",
        "Issue Tracker": "https://github.com/latk/yaxmldiff.py/issues"
    },
    "split_keywords": [
        "diff",
        " xml"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4ecbd0b7642c47e9c42dbfcd67af03746525f127981c8e81a62df30b78cf086f",
                "md5": "1c05227f2d5ddd0f04b55123931f1c5f",
                "sha256": "5377f4ab8ec949c7abb01e62443984b75ca0c319750268efe9d322b8b7f3c63f"
            },
            "downloads": -1,
            "filename": "yaxmldiff-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1c05227f2d5ddd0f04b55123931f1c5f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 9372,
            "upload_time": "2024-09-29T14:53:17",
            "upload_time_iso_8601": "2024-09-29T14:53:17.716524Z",
            "url": "https://files.pythonhosted.org/packages/4e/cb/d0b7642c47e9c42dbfcd67af03746525f127981c8e81a62df30b78cf086f/yaxmldiff-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c31edbc8d5cfa845c6e5a125562ccdfdc1bb26d84891e15f4b6de082add76a3d",
                "md5": "5e542e801f8f0babc0b228086611a8b6",
                "sha256": "ea2d92072bd3be93e4e896ae037f21e855d13cb8c3cd35215453b218bc800641"
            },
            "downloads": -1,
            "filename": "yaxmldiff-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "5e542e801f8f0babc0b228086611a8b6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 30530,
            "upload_time": "2024-09-29T14:53:20",
            "upload_time_iso_8601": "2024-09-29T14:53:20.442754Z",
            "url": "https://files.pythonhosted.org/packages/c3/1e/dbc8d5cfa845c6e5a125562ccdfdc1bb26d84891e15f4b6de082add76a3d/yaxmldiff-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-29 14:53:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "latk",
    "github_project": "yaxmldiff.py",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "yaxmldiff"
}
        
Elapsed time: 1.61286s