Skip to main content

A simple XML element sorter

Project description

sortxml - a simple XML element sorter

This module can be used by importing sortxml.sort_xml or by running standalone from the command-line.

Using sort_xml():

Returns an ElementTree representing the resulting whole document. ElementTree can easily be converted to string or written to a file like so:

    >>> foo_str = ET.tostring(sort_xml(xml_doc, node_path, sort_attr).getroot())
        # Or...
    >>> sort_xml(xml_doc, node_path, sort_attr).write('foo.xml')

Required arguments:

  • xml_doc -- a text IO stream (such as an open file object), Path object pointing to an XML file, string representing the file path, or string containing the file contents of a valid XML file. Can't take an ElementTree instance because we need to use our own parser to keep track of namespaces.
  • node_path -- a string containing the path to the node you want to sort the children of in the XPath language of the etree module
  • sort_attr -- the attribute of the child elements to use as the sort key

Optional arguments:

  • use_text -- use sort_attr as the name of a subelement of the path's children whose text will be the sort key (default: False)
  • sort_as_datetime -- try to parse the values of the sort key as a datetime using the dateutil module and sort chronologically (default: False, mutually exclusive with sort_as_decimal)
  • sort_as_decimal -- try to parse the values of the sort key as a decimal and sort numerically (useful to keep '10' from showing up right after '1') (default: False, mutually exclusive with sort_as_datetime)
  • descending -- sort in descending order instead of ascending (default: False)

Usage on the command line:

Run python -m sortxml -h to display this help text.

Usage: sortxml [-h] [-v] [-r] [-t] [--datetime | --decimal] [-o OUTPUT_FILE] input_file sort_xpath sort_attr

A simple XML element sorter. Will sort the children of selected elements using a given attribute's value or subelement's text as the sort key.
Example usage:

    $ python sortxml.py ARForm_orig.rdl "./DataSets/DataSet[@Name='ARForm']/Fields" Name -o ARForm.rdl

Positional arguments:

  • input_file – File path to the source xml file.
  • sort_xpath – XPath-style selector for elements to sort the children of. This has the same limitations as Python's ElementTree module.
  • sort_attr – The name of the attribute to use as the sort key.

Options:

  • -h, --help – show this help message and exit
  • -v, --version – show program's version number and exit
  • -r, --reverse, --descending – Sort the child elements in reverse (descending) order.
  • -t, --text, --use-text – Treat the sort attribute name as the name of a subelement whose text is the sort key.
  • --datetime, --as-datetime – Try to parse the sort key as a date/time value. Mutually exclusive with --decimal.
  • --decimal, --as-decimal – Try to parse the sort key as a decimal number. Mutually exclusive with --datetime.
  • -o OUTPUT_FILE, --output OUTPUT_FILE – File path to the destination file. (Default is to append '_sorted' to the filename before the extension.)

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

sortxml-0.1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

sortxml-0.1.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file sortxml-0.1.0.tar.gz.

File metadata

  • Download URL: sortxml-0.1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for sortxml-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6479b08e71dce6e23db28242d74b5fb7195ef0aa6c754d921c1e1fa2445b3a6e
MD5 afc379b366d76f34280f121649ddcc84
BLAKE2b-256 42a034b53be7faf96aca13a37b8039e61c8f1af02c27827b6f3bd060d30baa0d

See more details on using hashes here.

File details

Details for the file sortxml-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sortxml-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for sortxml-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0cd4441b6c30ed4b1ba0c5720164a3ccb934e8467527e03cd1bc319ba19b378b
MD5 f9eaff09195d7ba420459a5c2a22611f
BLAKE2b-256 3f7af3b53486dc4a8b3719a0a2e92e28fb8f1f5cfbffaab726f78a6f957d684a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page