Xelian
Xelian

Reputation: 17208

Is XML declaration needed in XSLT files?

I have the following XSLT

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"...

It does not start with XML declaration.

By standard:

Because each XML entity not accompanied by external encoding information and not in UTF-8 or UTF-16 encoding must begin with an XML encoding declaration, in which the first characters must be '< ?xml', any conforming processor can detect, after two to four octets of input, which of the following cases apply.

...and in section 2.8 we get the production for prolog:

[22]    prolog     ::=       XMLDecl? Misc* (doctypedecl Misc*)?
[23]    XMLDecl    ::=      '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'

So, we can omit the XML declaration, but we can't prefix it with anything.

So I can omit it and the transformation do well but is it a good practice? Can I hit unpredictable situation when have Cyrillic or Chinese symbols for example?

Upvotes: 1

Views: 223

Answers (1)

kjhughes
kjhughes

Reputation: 111621

Lexically, the XML declaration is optional for all XML files, including XSLT.

Semantically, it is required for XML 1.1 (and beyond) and encodings other than UTF-8 or UTF-16. (Technically, an XML declaration is still optional for other than UTF-8 or UTF-16 if an encoding is determined by a higher-level protocol, but for clarity of intent, you ought to use an XML declaration if possible consistently in such cases too.)

Upvotes: 4

Related Questions