How to use it
Under “Mode”, “Format” puts each element on its own line and indents it by its depth. Pick the indent from the list below it: “2 spaces”, “4 spaces”, or “Tab”. “Minify” removes the line breaks and indentation between elements to join everything onto one line, and the indent list disappears.
Here is a one-line input and what you get when you format it with 2 spaces.
<order id="7"><item sku="A1" qty="2">Pen</item><item sku="B2" qty="1"/><note> keep spaces </note></order>
<order id="7">
<item sku="A1" qty="2">Pen</item>
<item sku="B2" qty="1"/>
<note> keep spaces </note>
</order>
Elements that contain only text (item, note) stay on one line, and their text is kept exactly, spaces included. “Lines” and “Size” under the result measure the result, not your input, and Size is the UTF-8 byte count.
What stays as is and what gets rewritten
Comments (<!-- -->), CDATA sections (<![CDATA[ ]]>), and processing instructions (<?name ?>) keep their content unchanged. Format places each on its own indented line, and Minify only removes the line breaks around them. The XML declaration at the top (<?xml version="1.0"?>) and the <!DOCTYPE ...> declaration are carried over character for character, including everything inside them.
Elements and attributes are different. The browser parses the document and then writes them out again, so notations that mean the same thing come out in one consistent form.
| Input | Result |
|---|---|
<b></b> |
<b/> |
<c> </c> (an element containing only spaces) |
unchanged |
x='1' |
x="1" |
' |
' |
é, A |
é, A |
&, <, > |
unchanged |
The second row is the one to watch. An element that contains nothing but whitespace is not reduced to an empty element in either Format or Minify, and its whitespace is kept as it is. That holds even when it doesn’t carry xml:space="preserve". So in documents where whitespace is itself the value, for example an element that holds a single space as a separator, that value doesn’t change.
Entities you define yourself inside the DOCTYPE are expanded too. Define <!ENTITY co "ACME">, write &co; in the body, and the body of the result reads ACME, while the definition in the DOCTYPE stays where it was.
Format and Minify treat mixed text and elements differently
This is about elements that hold both text and child elements, like <p>Hello <b>big</b> world <i>x</i></p>. XML calls this mixed content. Format gives each piece of text its own line, just like an element, and trims the spaces around it.
<p>
Hello
<b>big</b>
world
<i>x</i>
</p>
That’s easy to read, but the single space that was between Hello and <b> has turned into a line break plus indentation. For XML where whitespace shows up on the page, as in document formats like XHTML or DocBook, use the formatted result for reading only and don’t save it over the original.
Minify does the opposite and leaves such elements alone. Inside an element with mixed content it keeps spaces and line breaks as they were, so the input above comes out of Minify unchanged, and if it had been spread over several lines, those line breaks would stay as well. What Minify removes is the indentation in places that contain only elements.
It checks syntax and nothing more
The tool only confirms that a document is well-formed XML. That means the grammar of XML itself: opening and closing tags match, attribute values are quoted, there is exactly one outermost element. Whether the document follows the structure laid down by a schema such as a DTD or XSD (which elements come in which order, which attributes are required) is the separate question of whether it’s valid, and the tool doesn’t check that. Getting a result tells you the syntax is right and nothing else.
When there’s a syntax error, you get a message such as “This isn’t well-formed XML. Check around line 1, column 20.” in place of the result, along with that line and a ^ marker. The examples below were checked in Chrome.
| Input | Position shown | Cause |
|---|---|---|
<a><b>1</b><c>2</b></a> |
line 1, column 20 | <c> is closed with </b> |
<a>1</a><b>2</b> |
line 1, column 9 | two outermost elements |
<a b="1" b="2"/> |
line 1, column 15 | the same attribute twice |
<x:a><x:b/></x:a> |
line 1, column 5 | no namespace declaration (xmlns:x) for the prefix x |
<?xml version="1.0"?> after two spaces |
line 1, column 8 | something comes before the XML declaration |
The last case comes up when you copy a file’s contents and a blank line or some spaces come along at the very top. The declaration has to start at the first character of the document.
Documents with external entities
When the DOCTYPE defines an entity that refers to an external file, the places that reference that entity can come out empty in the result.
Format <!DOCTYPE a [<!ENTITY e SYSTEM "http://example.invalid/e.txt">]><a>&e;</a> in Chrome and the DOCTYPE stays, but the body becomes <a/>. The spot that referenced the external entity is emptied without any error, so if you save the formatted result over the original of a document like this, the reference is gone. When the DOCTYPE declares an external entity, a warning about this appears below the result.
