jhyap
jhyap

Reputation: 3837

Is "#" legal in xml?

Apologize if I asked a noob question:

Is "#" legal in xml?

<?xml version="1.0" encoding="UTF-8"?>
<mmi_legend>
    <element value="1">
    <sky_blue>#00CCFF</sky_blue> //Is the "#" valid in xml?
    </element>
</mmi_legend>

I checked the reference list, it do not mention any character reference for " # ", so does this mean " # " is legal in xml?

Upvotes: 1

Views: 90

Answers (3)

Remy Lebeau
Remy Lebeau

Reputation: 598124

If you read the official XML spec, Section 2.2 defines a character as:

A character is an atomic unit of text as specified by ISO/IEC 10646:2000 [ISO/IEC 10646]. Legal characters are tab, carriage return, line feed, and the legal characters of Unicode and ISO/IEC 10646. The versions of these standards cited in A.1 Normative References were current at the time this document was prepared. New characters may be added to these standards by amendments or new editions. Consequently, XML processors MUST accept any character in the range specified for Char. ]

Character Range

Char    ::=    #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] /* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. */ 

Section 2.4 further says:

In the content of elements, character data is any string of characters which does not contain the start-delimiter of any markup and does not include the CDATA-section-close delimiter, " ]]> ". In a CDATA section, character data is any string of characters not including the CDATA-section-close delimiter, " ]]> ".

CharData    ::=    [^<&]* - ([^<&]* ']]>' [^<&]*) 

So, to answer your question - # is #x23, so it is allowed to appear unencoded in element data, per the definitions of Char and CharData.

Upvotes: 5

the # is perfectly legal, as per http://www.w3.org/TR/REC-xml/#dt-chardata but your // comment is most definitely not.

Upvotes: 3

John Wu
John Wu

Reputation: 52290

Yes, # is a legal character for use within an XML element and does not require escaping.

Upvotes: 5

Related Questions