Escape apostrophes in XML

When an XML apostrophe needs escaping, what ' and ' actually change, and how escaped text behaves when copied, searched or read aloud.

Escaping an apostrophe in XML is often treated as a routine step, but much of the time it isn't needed. This review explains when the escape is required, what it changes in the document, and what it doesn't change. It also covers how escaped text holds up when people copy, search or share it. The guidance relies on the XML specification's documented rules. No specific tool was tested for this article, so any claim about a particular editor, browser or screen reader would need its own check.

Is escaping apostrophes the right choice for your use?

Escape an apostrophe when it appears inside an attribute value that is wrapped in single quotes. There, an unescaped apostrophe ends the value early and breaks the document. Escaping also makes sense when you build XML by joining strings and can't control which quote character surrounds an attribute. In that case, escaping every apostrophe is a safe default.

Leave the plain apostrophe alone in element text and in attributes wrapped in double quotes. XML allows it in both places, and the plain character is easier for people to read and edit. The main tradeoff is readability. Escaping keeps the markup valid, but it makes the raw source harder to read and harder to search.

What escaping actually changes

XML defines five named entities, and ' is one of them. You can also write the apostrophe as a numeric character reference, either ' in decimal or ' in hexadecimal. All three forms change only how the character is written in the source file. A conforming parser turns each one back into the same single character, U+0027 APOSTROPHE. Software that reads the parsed document sees no difference between an escaped and an unescaped apostrophe.

Escaping is therefore a change to the stored characters, not to how anything looks. It has nothing in common with font styling, mirrored glyphs, CSS presentation or reversed text, even though some tool menus list it next to those features.

Form | Where it is defined | Parsed result
' (literal) | Any XML text | U+0027
' | One of the five predefined XML entities | U+0027
' | Decimal character reference | U+0027
' | Hexadecimal character reference | U+0027

The following examples show where the escape is required, where it is optional, and where a missing escape breaks the document.

<!-- Valid: plain apostrophe in element text -->
<p>It's ready.</p>

<!-- Valid: plain apostrophe inside a double-quoted attribute -->
<note author="O'Brien"/>

<!-- Valid: escaped apostrophe inside a single-quoted attribute -->
<note author='O&apos;Brien'/>

<!-- Not well-formed: the apostrophe ends the attribute value early -->
<note author='O'Brien'/>

Replacing the straight apostrophe with the typographic right single quotation mark (’, U+2019) is a different kind of change. It swaps one character for another, so the stored text is no longer the same. U+2019 never needs escaping in XML, provided the document's encoding can represent it. However, it won't match searches, comparisons or keys that expect U+0027.

Escaping in code

A library that serializes XML is usually safer than hand-written find-and-replace, because it escapes characters according to context. If you do escape by hand, replace the ampersand first. Otherwise you will escape the ampersands of the entities you just inserted, a problem known as double-escaping.

Python's standard library shows a common pitfall. By default, xml.sax.saxutils.escape escapes only &, < and >, so apostrophes pass through unchanged unless you add a mapping for them. By contrast, quoteattr picks a quoting style that suits the value, and html.escape writes the apostrophe as '.

from xml.sax.saxutils import escape, quoteattr

escape("O'Brien")                           # O'Brien (unchanged)
escape("O'Brien", {"'": "&apos;"})          # O&apos;Brien
quoteattr("O'Brien")                        # "O'Brien" (wrapped in double quotes)

CDATA sections allow a literal apostrophe in element content, but they can't be used inside attribute values. An apostrophe in an XPath query is a separate problem. It concerns the quoting of string literals in the query language, and escaping the XML document doesn't solve it.

Will the text stay readable and usable after copying or sharing?

  • Copy and paste: Copying from a rendered or parsed view usually gives a plain apostrophe. Copying from the raw source gives the literal text ' or ', which then shows up as-is in emails, chat messages and documents.
  • Search: A plain-text search of the raw file for don't won't find don't. Searches that run on the parsed document find both forms.
  • Double-escaping: If text is escaped twice, the source contains &apos;, and readers see ' on screen instead of an apostrophe. This is the most common visible failure.
  • HTML destinations: ' was not defined in HTML 4. Older HTML consumers, and XHTML served as ordinary HTML, may show it literally. The numeric form ' is the more widely accepted choice when the destination is uncertain.
  • Assistive technology: Screen readers work with the parsed text, so a correctly parsed apostrophe should read the same whether or not it was escaped. Raw source opened as plain text may be read character by character, entity included. No screen reader testing was done for this article, so check your specific setup if this matters.

Give people a plain-text version whenever they will read, quote or edit the content outside an XML-aware tool. Hand them the parsed output with ordinary apostrophes rather than the escaped source.

Gaps and limits

This article doesn't include verified results for any named converter, editor, browser or assistive technology. Its statements about escaping rely on the XML specification and documented library behavior. If you use an online escaping tool, test it on your own samples. Check that it escapes the ampersand correctly, doesn't double-escape, and leaves typographic quotes alone unless you want them changed. Don't make assumptions about how a tool handles the text you paste into it.