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

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

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: [email protected]. The smallest file that shows the problem is the most useful thing to send.

Back to the studio