เว็บและ HTML

การอ้างอิงอักขระ XML

เวอร์ชัน XML ของการอ้างอิงอักขระเชิงตัวเลข: ✓ หรือ ✓ XML มีเพียง 5 entity ที่มีชื่อ (& < > " ') ในขณะที่ HTML5 มี 2,231 รายการ

· Updated

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 -->
&#65;       A
&#x41;      A
&#169;      ©
&#xA9;      ©
&#x1F600;   😀

<!-- Named references in XML — only these 5 are built-in -->
&amp;   &
&lt;    <
&gt;    >
&quot;  "
&apos;  '

<!-- All others require a DTD declaration, e.g.: -->
<!DOCTYPE doc [
  <!ENTITY copy "&#xA9;">
]>
<doc>Copyright &copy; 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: &#x20AC; 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: &#x20AC;10 &#38; 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>&#x2190; Left &#x2192; Right</text>

<!-- Atom feed with special characters -->
<title>Q&#38;A: Unicode &#x2014; 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 "&#xA9;"; }
]]></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: &amp; &lt; &gt; &quot; &apos;
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

CSS Text Direction

CSS properties (direction, writing-mode, unicode-bidi) controlling text layout direction. Works with Unicode …

HTML เอนทิตี

การแทนค่าอักขระในรูปแบบข้อความใน HTML สามรูปแบบ: ชื่อ (&amp;), ทศนิยม (&#38;), เลขฐานสิบหก (&#x26;) จำเป็นสำหรับอักขระที่ขัดแย้งกับไวยากรณ์ HTML

Internationalized Domain Name (IDN)

ชื่อโดเมนที่มีอักขระ Unicode ที่ไม่ใช่ ASCII เก็บไว้ภายในเป็น Punycode (xn--...) แต่แสดงเป็น Unicode ให้ผู้ใช้เห็น ความกังวลด้านความปลอดภัย: การโจมตีแบบ …

JavaScript Intl API

ECMAScript Internationalization API providing locale-aware string comparison (Collator), number formatting (NumberFormat), date …

Punycode

การเข้ารหัสที่เข้ากันได้กับ ASCII สำหรับชื่อโดเมน Unicode แปลงป้ายกำกับที่ถูก internationalize เป็นสตริง ASCII ที่มีคำนำหน้า xn-- münchen.de → …

Unicode in CSS

CSS supports Unicode via escape sequences (\2713 for ✓), the content property …

การอ้างอิงอักขระที่มีชื่อ

HTML entity ที่ใช้ชื่อที่อ่านง่าย: &copy; → ©, &mdash; → — HTML5 กำหนด 2,231 …

การอ้างอิงอักขระเชิงตัวเลข

HTML entity ที่ใช้หมายเลข code point Unicode: ทศนิยม (&#169; → ©) หรือเลขฐานสิบหก (&#xA9; …

การเข้ารหัสเปอร์เซ็นต์ (URL encoding)

การเข้ารหัสอักขระที่ไม่ใช่ ASCII และอักขระที่สงวนไว้ใน URL โดยแทนที่แต่ละไบต์ด้วย %XX ใช้ UTF-8 ก่อน แล้วเข้ารหัส percent แต่ละไบต์: …

การแสดงข้อความ

การเรนเดอร์อักขระด้วย glyph ข้อความสีเดียวธรรมดาแทนที่จะเป็น emoji แบบสี มักใช้ Variation Selector 15 (U+FE0E) เพื่อแทนที่การแสดงผล emoji …