What formatting changes, and what it never touches
In data style XML, the line breaks and indentation between elements carry no meaning, so a formatter can rebuild them. Everything else is copied exactly:
- Text inside elements is never trimmed or re-wrapped.
<name> Ann </name>keeps its spaces. - Attribute values, entity references such as
&, CDATA sections, comments, processing instructions and the DOCTYPE are copied as written. - An element that mixes text and child elements, such as
<p>Hello <b>world</b></p>, is written back untouched, because the spaces around the tags are part of the text. - An element with
xml:space="preserve"is written back untouched.
Small tidy-ups are applied to tags: one space between attributes, no spaces around =, and <a /> written as <a/>. The Write empty elements as <a/> option also turns <a></a> into <a/>. Both forms mean the same thing.
Common XML errors and what they mean
XML is far stricter than HTML. These are the problems people run into most:
- Expected a closing tag, but found another. Tags must close in the reverse order they opened. The message names the tag that is still open and where it began.
- Names are case sensitive.
<Item>and</item>do not match. - This & does not start a valid entity. A literal ampersand must be written
&. It is common in URLs, such as?a=1&b=2. - Unknown entity . XML defines only
& < > " '. Use the character itself or a number such as . - An attribute has no value, or its value is not in quotes.
<input disabled>and<a href=x>are HTML habits. XML needsdisabled="disabled"andhref="x". - A namespace prefix is not declared.
<a:item>needsxmlns:a="..."on it or on an element around it. - The XML declaration must be first. A blank line or space before
<?xmlis an error. - A second root element. An XML document has one root element that holds everything else.
XML, SVG and HTML
SVG, RSS and Atom feeds, GPX tracks, Android layouts, Maven pom.xml and many config files are XML, so they format here. HTML is not XML: it allows unclosed tags such as <br> and <li>, so use the HTML formatter for web pages. Pages written as strict XHTML are XML and work in both.
Minifying XML
Minify removes the white space between elements and, by default, comments. Text, mixed content and xml:space="preserve" sections are left as written, so the document means the same. It is useful for payloads and for storing XML in a field with a size limit. For files people edit, keep them formatted.