XML Escape / Unescape · Tutorial

How to put special characters into XML safely

4 min readbeginnerUpdated

A product name contains an ampersand, angle brackets and quotes, and your XML feed breaks whenever it is included. You will escape it for an element and for an attribute, and check the result.

What you need

Steps

  1. Paste the value

    Load the example: “Fish & Chips <special offer> for "only" €9.50 — it's back!”.

  2. Escape for an element

    With Escape for XML selected, & and < and > are replaced. The result can go between tags.

    <name>Fish &amp; Chips &lt;special offer&gt; for &quot;only&quot; €9.50 — it&apos;s back!</name>
  3. Escape for an attribute

    Keep Escape quotes ticked when the value goes into an attribute such as title="…", so the quotes inside can’t end the value early.

  4. Check it

    Paste the finished element into the XML Viewer, Formatter & Validator. It should pass.

Try it yourself

Opens the XML Escape / Unescape with this tutorial's sample loaded.

Try it now

Common mistakes

Escaping twice
Escaping already escaped text turns &amp; into &amp;amp;. Unescape first if you are not sure.
Escaping a whole document
Escape only the values you insert. Escaping the tags themselves turns the markup into text.

Next steps

Go back to the XML Escape / Unescape, or keep going with one of these:

Tutorial · 3 min · beginner

How to decode escaped text from XML

Turn &lt;, &amp; and numeric references back into readable characters, for example to read HTML stored inside an XML element.