#!/usr/bin/env pmpython
#
# Copyright (C) 2018 Ken McDonell.
#
# Solution liberally borrowed from answer provided by
# https://meta.stackoverflow.com/users/267781/matth at
# https://stackoverflow.com/questions/8385358/lxml-sorting-tag-order
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Iostat Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
""" Sort XML """

import sys
from lxml import etree

# load and parse the XML
tmp = etree.parse(sys.stdin)

# walk the tree, sorting elements
for parent in tmp.xpath('//*[./*]'):
  parent[:] = sorted(parent,key=lambda x: x.tag)

# spit it out
print (etree.tostring(tmp,encoding='utf-8',pretty_print=True).decode())

sys.exit(0)
