About
Tool No. 57 of Pikkola
An XML document carries two things at once: a shape, which is elements inside elements, and text, which is what a reader finally sees. A tool that keeps the two apart can indent, query and convert the same file without arguing with it. This page does all four jobs in the tab.
What it does
- Indents a document with two spaces, four spaces or a tab, or removes the whitespace between elements and writes it as one line.
- Checks well formedness while you type: unclosed and mismatched tags, unquoted attribute values, repeated attributes, undeclared entities, a second root, and a comment, CDATA block or doctype that never closes. Each finding carries a line and a column.
- Runs an XPath 1.0 expression and lists what it matched: the node, its path, its text or attribute value. Pressing a result selects the node in the source.
- Writes the document as JSON and reads JSON back as XML, with attributes under @ and text under #text.
- Five examples shaped like real files: an RSS feed, a sitemap, an SVG, a Maven pom.xml and an Android manifest.
- Copy, download, three languages and a dark theme.
How the scanner works
The document is read once, in order, by a hand written scanner. It keeps a stack of the elements that are open, so a closing tag can be compared with the top of the stack instead of being searched for; it keeps a namespace scope per open element, so a prefix can be checked where it is used; and it records the start and end of every tag, attribute, text run, comment and CDATA block as it goes. Those offsets are what let a finding select the exact characters it is about, and what let an XPath result jump back into the source.
The browser's own XML parser then builds the tree used for formatting, XPath and JSON. It is asked only after the scanner is satisfied, so a document that reaches it is one the scanner could read to the end. If the two ever disagree, the message from the browser parser is shown with its line and column, and the result is withheld rather than guessed.
What the formatter decides
Whitespace between elements is layout, so it is thrown away and written again at the chosen width. Whitespace inside a text node is content, so a text node keeps every character it had, and an element whose only children are text stays on one line. Mixed content, where text and elements sit side by side, is left exactly as it was written, because re-indenting it would change what a reader sees.
CDATA keeps its form, comments keep their text, a processing instruction keeps its data, and the doctype is carried through as it was written. Minify removes the whitespace between elements only; it does not touch text, and it does not remove comments.
How a document becomes JSON
An attribute becomes a key that starts with @, so id="42" becomes "@id": "42". Text becomes the value of the element when nothing else sits beside it, and goes under #text when attributes or child elements are present. Siblings with the same name become an array, in the order they appeared. The root element becomes the single key of the object, because JSON has no document node above it.
Going the other way, a key that starts with @ becomes an attribute, #text becomes text, an array becomes a repeated element, and a null becomes an empty element. A root object has to have exactly one key, since XML allows one root element; the page says so rather than inventing a wrapper.
Where the limits are
- XPath 1.0, as the browser implements it. That means no
matches(), noforexpression and no default namespace for name tests, which is why a sitemap needs//*[local-name()='loc']. - Well formedness, not validity. No XSD or DTD is fetched or applied, and no external entity is resolved, because nothing is fetched at all.
- A default namespace is reported as a warning only when a prefix is used and never declared. A document that declares a prefix and never uses it is legal and is left alone.
- JSON has no comments and no attribute order of its own, so comments are counted and left out of the JSON view.
- Mixed content becomes an object whose #text holds the joined text, which is the best a tree of keys can do without inventing an order.
- Nothing is uploaded, so a document that refers to an external DTD cannot be validated against it here.
Why it is free
Pikkola builds small tools with one job each and keeps them free, with no account and no lock on the result. A static page that works in the browser costs almost nothing to serve, so there is no reason to charge for it or to watch who uses it.
Contact
A document this page refuses but another parser accepts, a node list that reads in an unexpected order, a Korean or Japanese sentence that reads badly: hello@pikkola.com. The smallest file that shows the problem is the most useful thing to send.