XML 문자 참조
XML 버전의 숫자 문자 참조: ✓ 또는 ✓. XML에는 명명된 엔티티가 5개(& < > " ')뿐이지만 HTML5는 2,231개를 가집니다.
What Are XML Character References?
XML character references are escape sequences that represent Unicode characters by code point within XML documents. The XML specification defines two forms:
- Decimal:
&#N;— e.g.,©for © - Hexadecimal:
&#xH;— e.g.,©for ©
Unlike HTML, XML does not support named references beyond the five predefined entities (&, <, >, ", '). All other characters must be referenced by number or defined as custom entities in the document's DTD.
XML vs. HTML Character References
HTML inherits XML-style numeric references but extends them with thousands of named references defined in the HTML specification. XML keeps a stricter, self-contained model: a parser needs no external lookup table to handle character references, only the rules for decimal and hex integers.
<!-- Valid XML character references -->
A A
A A
© ©
© ©
😀 😀
<!-- Named references in XML — only these 5 are built-in -->
& &
< <
> >
" "
' '
<!-- All others require a DTD declaration, e.g.: -->
<!DOCTYPE doc [
<!ENTITY copy "©">
]>
<doc>Copyright © 2024</doc>
Well-Formedness Rules
For XML to be well-formed, character references must:
1. Reference a legal XML character — code points U+0009, U+000A, U+000D, U+0020–U+D7FF, U+E000–U+FFFD, and U+10000–U+10FFFF.
2. End with a semicolon ; — the semicolon is always mandatory in XML (no legacy exceptions).
3. Not reference surrogates (U+D800–U+DFFF) or the null character (U+0000).
An XML parser that encounters an invalid reference is required to raise a fatal error and halt parsing.
Encoding Interaction
XML documents declare their encoding in the XML declaration:
<?xml version="1.0" encoding="UTF-8"?>
Character references bypass encoding: € always means U+20AC (€) regardless of the document's encoding. This makes character references useful in legacy encodings that cannot natively represent the desired characters.
Parsing in Python
import xml.etree.ElementTree as ET
xml_str = "<item>Price: €10 & shipping</item>"
root = ET.fromstring(xml_str)
print(root.text) # "Price: €10 & shipping"
# Python's xml module decodes references automatically
SVG and XML-Based Formats
SVG, MathML, XHTML, RSS, and Atom are all XML vocabularies. Character references work identically in all of them:
<!-- SVG text with Unicode arrows -->
<text>← Left → Right</text>
<!-- Atom feed with special characters -->
<title>Q&A: Unicode — Explained</title>
CDATA Sections
Inside a CDATA section (<![CDATA[ ... ]]>), character references are not processed — the text is treated as raw character data. This is useful for embedding code samples in XML:
<code><![CDATA[
if (a < b && c > d) { return "©"; }
]]></code>
<!-- The & and < above are literal characters, NOT entities -->
Quick Facts
| Property | Value |
|---|---|
| Decimal syntax | &#N; |
| Hex syntax | &#xH; (lowercase x required) |
| Named entities (built-in) | Only 5: & < > " ' |
| Semicolon | Always mandatory in XML |
| Null character U+0000 | Forbidden |
| Surrogates | Forbidden |
| CDATA sections | References are not expanded inside <![CDATA[...]]> |
| Error handling | Fatal error on invalid reference (unlike HTML's lenient parsing) |
관련 용어
웹 & HTML의 더 많은 용어
응답의 문자 인코딩을 선언하는 HTTP 헤더 매개변수(Content-Type: text/html; charset=utf-8). 문서 내 인코딩 …
::before 및 ::after 의사 요소를 통해 유니코드 이스케이프를 사용하여 생성된 콘텐츠를 삽입하는 …
CSS properties (direction, writing-mode, unicode-bidi) controlling text layout direction. Works with Unicode …
HTML에서 문자를 텍스트로 표현하는 방식. 세 가지 형태: 이름(&), 십진수(&), 16진수(&). HTML …
ECMAScript Internationalization API providing locale-aware string comparison (Collator), number formatting (NumberFormat), date …
유니코드 도메인 이름을 ASCII 호환 인코딩으로 변환하여 xn-- 접두사가 붙은 ASCII 문자열로 …
CSS supports Unicode via escape sequences (\2713 for ✓), the content property …
비ASCII 유니코드 문자를 포함하는 도메인 이름으로, 내부적으로는 Punycode(xn--...)로 저장되지만 사용자에게는 유니코드로 표시됩니다. …
U+2060. 줄 바꿈을 방지하는 너비 없는 문자. 너비 없는 줄 바꿈 없는 …
사람이 읽기 쉬운 이름을 사용하는 HTML 엔티티: © → ©, — → …