How to Encode an Em Dash in XML
Write an em dash (U+2014) in XML as a literal character or as — or —, avoid the undeclared — error, and check the result with a parser.
The em dash (—) is the Unicode character U+2014 EM DASH. XML gives you two reliable ways to write it. You can type the character directly in a UTF-8 or UTF-16 document. You can also use a numeric character reference: — in decimal or — in hexadecimal. The HTML-style named entity — is not built into XML, and a parser will reject it unless a DTD declares it.
What encoding an em dash changes in your text
Encoding the dash means choosing how the character is written in the file. Every valid form produces the same single character, U+2014, once a parser reads the document. The choice changes the raw bytes and how readable the source is, but it does not change the meaning. Character references work in element content and in attribute values. They are not expanded inside comments or CDATA sections, where they stay as literal text.
Ways to write an em dash in XML
| Form | Example | Works in plain XML? | Notes |
| Literal character | — | Yes, if the declared encoding can represent it, such as UTF-8 or UTF-16 | Most readable. Stored as the bytes E2 80 94 in UTF-8 |
| Decimal reference | — | Yes, in any encoding | Uses only ASCII characters |
| Hexadecimal reference | — | Yes, in any encoding | The x must be lowercase. The hex digits can be either case |
| Named entity | — | Only if a DTD declares it | XML predefines only & < > " ' |
Don't confuse it with similar characters
| Character | Name | Code point | Decimal reference | Hex reference |
| — | Em dash | U+2014 | — | — |
| – | En dash | U+2013 | – | – |
| • | Hyphen-minus (keyboard hyphen) | U+002D | Not needed | Not needed |
| − | Minus sign | U+2212 | − | − |
How to add an em dash and check the result
Before you start, find out which encoding the file uses. Keep a copy of the original, or write your changes to a new file, so you can compare the two versions and restore the original if needed. The commands below assume bash, GNU sed, xmllint from libxml2 and Python 3. They are examples and have not been run against your files.
Step 1: Check the declared encoding
<?xml version="1.0" encoding="UTF-8"?>
Look for this declaration on the first line. If there is no declaration and no byte order mark, parsers treat the file as UTF-8.
If the encoding is UTF-8 or UTF-16, you can use the literal dash, as long as your editor saves the file in that same encoding.
If the declaration names US-ASCII or ISO-8859-1, use a numeric reference instead, because neither encoding includes the em dash.
If you see garbled text such as — where a dash should be, the file's actual encoding and its declared or assumed encoding don't match. That sequence is what the UTF-8 bytes of an em dash look like when they are read as Windows-1252.
Step 2: Insert the dash
In this example, the input uses two hyphens as a stand-in for a dash. Each of the three replacements below gives the same parsed text.
Before (example):
<note>Draft--review pending</note>
After (three equivalent options):
<note>Draft—review pending</note>
<note>Draft—review pending</note>
<note>Draft—review pending</note>
In an attribute value:
<item label="Q1—Q2"/>
Step 3: Declare — only if you need the name
If your content already uses —, for example text copied from HTML, either replace it with a numeric reference or declare the entity in the document's internal DTD subset.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ENTITY mdash "—">
]>
<note>Draft—review pending</note>
Numeric references are the more portable choice. Some parsers are set up to reject DTDs for security reasons. Tools that strip or ignore the DOCTYPE will also report — as an undefined entity.
Step 4: Check that the file is well-formed
xmllint --noout note.xml
If the command prints nothing, the file parsed without errors. If you used an undeclared —, xmllint reports an undefined-entity error that points to the line.
Step 5: Confirm the parser sees U+2014
python3 -c "import xml.etree.ElementTree as ET; t = ET.parse('note.xml').getroot().text; print([hex(ord(c)) for c in t if ord(c) > 127])"
This command lists every non-ASCII character in the root element's text. For the note example, the expected output is ['0x2014'] whether you used the literal character, — or —.
If you see 0x2013, you entered an en dash. If you see characters such as 0xe2 or 0x20ac, the file was decoded with the wrong encoding.
To count literal UTF-8 em dashes in the raw file without parsing it, use the command below.
grep -o $'\xe2\x80\x94' note.xml | wc -l
Converting existing em dashes in bulk
To make a file ASCII-safe, replace each literal em dash with a reference. Write the result to a new file. In sed, a bare & in the replacement stands for the matched text, so it must be escaped as &. Setting LC_ALL=C makes sed match the three UTF-8 bytes one at a time.
LC_ALL=C sed 's/\xe2\x80\x94/\—/g' input.xml > output.xml
diff input.xml output.xml
xmllint --noout output.xml
Plain text replacement does not understand XML structure. It also changes dashes inside comments, processing instructions and CDATA sections.
Inside CDATA this changes the content, because — there is read as seven literal characters, not as a dash. Review the diff for any CDATA sections before you replace the original file.
Another approach is to re-serialize the file with an XML library using an ASCII output encoding. Most libraries then write non-ASCII characters in text and attributes as numeric references. Check what your library preserves first. Python's ElementTree, for example, drops comments and the DOCTYPE by default and may change formatting.
Common problems and fixes
- Undefined entity error: — was used without a declaration. Replace it with — or declare it in a DTD.
- Garbled — in place of the dash: the file's bytes and its declared encoding don't match. Fix the declaration or re-save the file in the declared encoding.
- The reference appears literally in the output: it sits inside CDATA or a comment, or it was escaped twice and stored as —.
- A reference without its semicolon, such as —, makes the document not well-formed. Add the semicolon.
- A double hyphen used as a dash inside a comment: XML forbids two hyphens in a row inside comments. Use a literal em dash there, because references are not expanded in comments.