4

I have a basic understanding of XML. My question is why is it necessary to mention the encoding used at the beginning of a XML document and why encoding is required ?

1

2 Answers 2

3

It is not required, although usually you may want to include it:

In the absence of external character encoding information (such as MIME headers), parsed entities which are stored in an encoding other than UTF-8 or UTF-16 must begin with a text declaration (see 4.3.1 The Text Declaration) containing an encoding declaration.

So, for example, when transferring XML via HTTP, XML parser might use the value from Content-Type header like this:

Content-Type application/xml; charset=UTF-8

But once this document is stored locally, it would not contain this information - thus it seems like a good idea to include encoding into the declaration part of XML document.

Sign up to request clarification or add additional context in comments.

1 Comment

Unlike the VersionInfo, the EncodingDecl in the BNF grammar notation does NOT look optional.
1

why is it necessary to mention the encoding used at the beginning of a XML document

It isn't. There are defaults. (UTF-8 and UTF-16, which can be reliably distinguished between programatically)

and why encoding is required

Computers only understand binary. Encoding is the process of representing letters, numbers, etc in binary so it can be processed by a computer. Different encodings store the characters in different ways.

10 Comments

This means I need not mention encoding in the <?xml tag ?
So long as the encoding is UTF-8 or UTF-16.
@Quentin, is that true? From the spec: `TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>' - I think EncodingDecl can me ommited only if the whole XML delcaration is being ommited.
@AmritaS, <?xml is not a tag per se, it's an XML declaration.
@Quentin Even the version attribute can be omitted ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.