Sort XML Online
FV-41Paste an XML document and sort its child elements and attributes by name, A to Z or Z to A. Comments, CDATA, attribute values and text are kept, and the result comes back indented.
Runs in your browser. Nothing is uploaded.
XML document
Sorted XML
How to use it
- Paste your XML document into the input box.
- Choose A to Z or Z to A, and whether to sort at every level or only the children of the root.
- Keep Sort attributes by name ticked if attributes should be ordered too. Pick a layout: 2 spaces, 4 spaces, a tab or minified.
- Press Sort XML, then copy the result or download it as an .xml file.
What gets sorted
Child elements are sorted by element name, and attributes by attribute name, comparing by Unicode order unless Ignore case is on. Elements with the same name keep their original order, or are ordered by their attributes if you choose that. Comments and processing instructions that sit before an element travel with it. An element that mixes text with child elements is left in its original order, because the order is part of its meaning, but the elements inside it are still sorted.
What is preserved
Attribute values, text and CDATA sections are kept exactly as written, and the XML declaration and DOCTYPE stay at the top. The document is checked first, so a malformed document stops with the line and column of the problem instead of producing broken output. The document must have a single root element, and entities other than the five standard ones and numeric references such as © are rejected.
Layout of the result
Whitespace between elements is discarded and the result is laid out by the same XML formatter as the XML Formatter tool. It puts text and CDATA on their own lines, collapses runs of whitespace inside text and writes an empty element as a self-closing tag. If you need the original whitespace inside text kept, sort a copy and merge by hand. Sorting changes only the order of siblings; it does not change what the XML means for tools that read elements by name.
Error messages and what to do
| Message | What it means | Fix |
|---|---|---|
No root element found. XML needs one element such as <root>...</root> | The input box is empty or holds no element. | Paste a complete XML document with one root element. |
Questions people ask
How do I sort XML alphabetically?
Paste the XML and press Sort XML. Child elements are ordered by name and attributes by attribute name, at every level, and the result is indented. Choose Z to A for the reverse order.
Does sorting keep my comments?
Yes. A comment or processing instruction placed before an element moves together with that element. A comment after the last element of its parent stays at the end of the parent.
Can it sort by an attribute value instead of the element name?
Not directly. Elements are sorted by name. When several elements share a name, choose Order by attributes under Elements with the same name to order them by their attribute names and values.
Why was an element's content not reordered?
An element that mixes text and child elements, such as a paragraph with bold words, is never reordered, since moving elements around would change the text. Its own children are still sorted.
Does sorting change the meaning of the XML?
It can, for any application that depends on element order, such as lists, steps or documents. Check the order rules of your format first. Data that is read by element name, like many config files, is not affected.
About this tool
Child elements are sorted by element name, and attributes by attribute name, comparing text by Unicode order unless you tick Ignore case. The XML is checked first and a malformed document stops with the line and column. Comments and processing instructions that sit before an element move with it. Elements that mix text and child elements are left in their original order, because the order is part of their meaning, but their children are still sorted. Equal names keep their input order unless you choose to order them by attributes. Attribute values, text and CDATA are kept as written. Whitespace between elements is discarded and the result is re-indented by the XML formatter, which puts text on its own line, collapses runs of whitespace inside text and writes empty elements as a self-closing tag. A DOCTYPE is kept but not expanded, and entities other than the five standard ones and numeric references are rejected. Nothing is uploaded.