XML Formatter

Runs locally

Pretty-print, minify and check XML, with a collapsible tree and XPath.

Minify also drops comments and whitespace between tags.

XML

454 B

Well-formed · 13 elements, 7 attributes, depth 4

Formatted

<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns:dc="http://purl.org/dc/elements/1.1/">
<book id="bk101" lang="en">
<dc:title>XML Developer's Guide</dc:title>
<author>Gambardella, Matthew</author>
<price currency="USD">44.95</price>
<tags>
<tag>xml</tag>
<tag>reference</tag>
</tags>
</book>
<book id="bk102" lang="de">
<dc:title>Midnight Rain</dc:title>
<author>Ralls, Kim</author>
<price currency="EUR">5.95</price>
<!-- out of print -->
<tags/>
</book>
</catalog>
 

XPath

Runs in your browser's XML engine (XPath 1.0). A default namespace is available as the prefix d:

Attribute search

Find attributes by name or value, with the element path to each.

Guide

Well-formed XML is the check every parser makes first

An XML document is well-formed when every start tag has a matching end tag, elements nest without overlapping, attribute values are quoted, there is exactly one root element, and every & or < in text is escaped. A parser that finds any of these problems must stop, which is why a single stray ampersand in a config file breaks a whole deployment. This formatter checks exactly those rules and reports the first failure with its line and column, then pretty-prints the document when it passes.

Well-formed is not the same as valid. Validity means the document also matches a schema (XSD) or DTD — the right elements in the right order. That needs the schema, so this page does not claim it; it tells you the file will parse, which is the question behind most "invalid XML" errors. Text that is only whitespace between elements is dropped when formatting, and text inside an element is kept, so mixed content like <p>Hello <b>you</b></p> should be checked after formatting. To escape a value before pasting it into an element, use the HTML entity encoder, which applies the same five escapes XML needs.

XPath runs in your browser’s built-in XML engine (XPath 1.0), so expressions such as //book[price > 10]/@id or count(//item) work as they would in most server libraries. Namespaced documents are common in SOAP and Maven files: declared prefixes resolve as written, and a default namespace is available as d:, since XPath 1.0 cannot match it without a prefix.

Minified XML

<order id="7"><item sku="A1" qty="2"/><note>Leave at door &amp; ring</note></order>

Pretty-printed

<order id="7">
  <item sku="A1" qty="2"/>
  <note>Leave at door &amp; ring</note>
</order>

Write "door & ring" without &amp; and the formatter stops at that character: Unescaped "&" — write &amp; (or use a CDATA section).

Where people get caught

An unescaped & in text or a URL

Query strings in XML (…?a=1&b=2) must be written with &amp;. It is the most common reason a hand-edited config stops parsing.

Whitespace before the XML declaration

<?xml version="1.0"?> must be the very first bytes of the file. A blank line or a byte-order mark from an editor in front of it is a fatal error for strict parsers.

Default namespaces and XPath

In a document with xmlns="…", //project matches nothing in XPath 1.0. Use the prefix: //d:project here, or declare a prefix in your own code.

Formatting mixed content

Indenting adds whitespace around child elements. For XHTML or DocBook, where text and tags mix, that can add visible spaces; keep those files minified or format by hand.

About XML formatting

How it works in 4 steps · 4 common use cases · 4 questions answered

How it works

  1. 1.Paste or drop an XML file; it is checked and formatted as you type.
  2. 2.Choose the indent or minify, which also drops comments.
  3. 3.Switch to Tree to expand and collapse elements.
  4. 4.Run XPath or search attributes in the panels below.

Common use cases

  • •Reading a SOAP or legacy API response
  • •Finding the stray character that breaks a pom.xml or web.config
  • •Extracting values from a feed or sitemap with XPath
  • •Minifying an SVG or a config before embedding it

FAQ

What is the difference between well-formed and valid XML?

Well-formed means the syntax is correct and any parser can read it. Valid means it also matches a DTD or XSD schema. This tool checks well-formedness; schema validation needs the schema file.

Why does my XML fail with an "&" error?

A bare & starts an entity reference. Write &amp; for a literal ampersand, including inside URLs, or wrap the text in <![CDATA[ … ]]>.

Which XPath version does it support?

XPath 1.0, the version built into browsers and supported by most server libraries. Functions such as count(), contains() and position predicates work.

Does minifying change the data?

It removes whitespace-only text between elements and comments. Text inside elements and all attributes are kept.