Why format XML
XML that machines write is often one long line. SAML messages, metadata files and many configuration exports arrive that way. Reading them, comparing them or finding one attribute inside them is slow until the elements are on their own lines and indented by depth.
Minifying does the opposite: it removes the whitespace between elements to make a document smaller, for example before Base64-encoding it or putting it in a single-line setting.
Well-formed XML in short
A parser accepts XML only if it is well formed. The main rules:
- Exactly one root element.
- Every start tag has a matching end tag, or the element is self-closing like
<a/>. - Elements nest properly:
<a><b></b></a>, never<a><b></a></b>. - Attribute values are quoted, and an element has each attribute at most once.
<and&in text are written as<and&.- Namespace prefixes such as
saml:are declared withxmlns:saml="…"on the element or an ancestor.
Well formed is not the same as valid. A valid document also follows a schema, such as the SAML schemas, which define which elements may appear where. This tool checks well-formedness only.
What this tool does
Paste XML and pick Pretty or Minify. The tool parses the text with your browser’s XML parser, never as HTML, and prints it back.
- Pretty puts each element on its own line, indented by two spaces, four spaces or a tab.
- Minify removes the whitespace-only text between elements.
- Text inside an element is kept exactly as it was, so values are never changed.
- Comments can be kept or dropped. The XML declaration at the top is kept.
- Badges show the root element, the number of elements and the number of namespaces.
If the XML does not parse, the tool shows the browser’s error with the line and column where parsing stopped.
<a><b x="1">text</b><c/></a>
<a>
<b x="1">text</b>
<c/>
</a>Comments, CDATA and processing instructions
Comments are kept by default; turn off Keep comments to drop them. CDATA sections are printed back exactly, since their content is text that must not change. Processing instructions, such as a stylesheet reference, are kept as they were.
What it does not do
It does not validate against a schema, resolve external entities, or fetch anything a document points to. A document type declaration is kept by name, but its internal subset is not printed. It does not sort attributes or rewrite namespace prefixes.
How to use it
- Paste the XML.
- Pick Pretty or Minify, and for Pretty the indent.
- If an error appears, go to the line and column it names and fix the first problem. Later errors often disappear with it.
- Copy the result.
Formatting breaks XML signatures
This is the most important caveat for SAML. An XML signature covers the canonical form of the signed element. Canonicalization normalizes some things, such as attribute order and namespace declarations, but whitespace between elements stays part of the signed content.
So once a SAML response, assertion or metadata file is signed, adding or removing line breaks inside the signed element changes its digest and the signature no longer verifies. Format a copy to read it, but always send, store or import the original bytes.
If a signature fails only after someone “cleaned up” a metadata file or a response in an editor, this is usually why.
Use cases
Reading a decoded SAML message
The SAML response decoder and SAML request decoder already print their XML indented. For XML from other places, such as logs or a library’s debug output, paste it here.
Reviewing metadata
Metadata from an identity provider is often minified. Format it to see the endpoints and keys, or use the metadata viewer, which lists them for you.
Comparing two documents
Format both with the same indent, then compare them line by line in a diff tool. Differences in attributes and values stand out once the layout is the same.
Preparing XML for a one-line setting
Some consoles and environment variables take XML on one line. Minify it here first, then check that it still parses. Keep in mind that minifying a signed document breaks its signature, just like formatting does.
Finding why a file will not import
When an importer says a file is invalid, check well-formedness here first. If it parses, the problem is in the content, such as a wrong namespace or a missing required element.
Common parse errors
| Error | Usual cause |
|---|---|
| Extra content at the end of the document | More than one root element, or text after the root |
| Opening and ending tag mismatch | Tags closed in the wrong order, or a typo in a tag name |
| Namespace prefix is not defined | A prefix such as saml: used without its xmlns:saml declaration |
| EntityRef: expecting ; | A bare & in text or in an attribute value |
| Attribute redefined | The same attribute twice on one element |
| Start tag expected | Empty input, or text that is not XML, such as Base64 |
The exact wording depends on your browser. Chrome, Edge, Firefox and Safari all report the position.
XML and security
XML parsers have a history of security problems: external entities that read local files, entity expansion that uses all memory, and in SAML, signature wrapping. Browsers’ XML parsers do not load external entities, and this tool never renders the XML as a page, so pasting a document here is safe.
In your own services, parse SAML with a library that disables document type declarations and external entities, and verifies signatures on exactly the element it then reads.
Best practices
- Keep the original bytes of anything signed; format only copies.
- Use UTF-8 and say so in the XML declaration.
- Keep namespace prefixes consistent across your own documents to make diffs readable.
- Do not hand-edit generated metadata; regenerate it.
Namespaces in SAML documents
SAML documents mix several vocabularies, and XML namespaces keep them apart. The prefix before the colon is only a local nickname; what counts is the namespace URI it is bound to with xmlns:.
| Usual prefix | Namespace URI | Used for |
|---|---|---|
samlp | urn:oasis:names:tc:SAML:2.0:protocol | Requests and responses |
saml | urn:oasis:names:tc:SAML:2.0:assertion | Assertions, issuers, attributes |
md | urn:oasis:names:tc:SAML:2.0:metadata | Metadata |
ds | http://www.w3.org/2000/09/xmldsig# | Signatures and key info |
xenc | http://www.w3.org/2001/04/xmlenc# | Encrypted data |
Two documents can use different prefixes, or none, for the same namespace and mean exactly the same thing. Software must compare namespace URIs, not prefixes. When you compare two formatted documents and only the prefixes differ, the content is the same.
Encoding, line endings and the declaration
The XML declaration, <?xml version="1.0" encoding="UTF-8"?>, is optional for UTF-8 documents, which is what SAML uses. The tool keeps it when it is there and does not add one when it is not.
XML parsers normalize line endings to a single line feed, so a document saved on Windows and one saved on Linux read the same. A byte order mark at the start is allowed for UTF-8 but some older tools choke on it; the tool ignores it on input.