HTML to XML Converter

Rewrite loose HTML as strict XML that an XML parser will actually accept.

HTML
XML
Runs locally in your browser

About the HTML to XML Converter

HTML forgives a great deal. Tags can be left open, attribute values can go unquoted, <br> never closes and disabled needs no value. XML forgives none of it, which is why feeding a scraped page to an XML parser or an XSLT stylesheet usually ends in a parse error on line one. This converter reads the input the way a browser does and writes it back out under the stricter rules.

Four things change on the way through. Every element gets a closing tag or a self closing slash, so void elements come out as <br /> and <img ... />. Attribute values are quoted and escaped, and a valueless attribute is expanded to the XHTML form where the name repeats as the value, giving disabled="disabled". Script and style bodies are wrapped in CDATA so an unescaped ampersand or less than sign inside JavaScript does not break the document. Text nodes have their five significant characters escaped.

XML also insists on exactly one root element. When the input is a full page the html element serves; when it is a fragment with a single outer element that element becomes the root; and when it is a loose run of siblings they are wrapped in a container whose name you set in Wrapper name. Before the result is handed over it is parsed again as XML, and if the parser objects the tool reports the error instead of giving you something that only looks right. Add the XHTML namespace when the consumer expects it. To go the other way, XML to HTML and the XSL transformer take over.

How to use

  1. Paste the HTML. Broken nesting is repaired by the parser before conversion.
  2. Set Wrapper name if your fragment has several top level elements that need a container.
  3. Turn on Add the XHTML namespace when the target system expects namespaced markup.
  4. Press Convert; the output is validated as XML before it reaches the pane.

Common questions

Is the output the same as XHTML?
Close. Add the namespace option and it is XHTML in all but the doctype declaration, which you can paste in yourself if a validator asks for one.
Why is my script wrapped in CDATA?
JavaScript regularly contains characters that XML treats as markup. CDATA lets them through untouched. Turn the option off if your consumer strips CDATA sections.
What happens to HTML comments?
They are kept, with any run of hyphens reduced to one, because a double hyphen inside a comment is illegal in XML.
Are attribute names ever changed?
Only when they are not valid XML names. Characters XML forbids are removed, and an attribute that cannot be salvaged is dropped rather than producing a broken document.