XML - Declaration



This chapter covers XML declaration in detail. XML declaration contains details that prepare an XML processor to parse the XML document. It is optional, but when used, it must appear in the first line of the XML document.

Syntax

Following syntax shows XML declaration −

<?xml
   version = "version_number"
   encoding = "encoding_declaration"
   standalone = "standalone_status"
?>

Each parameter consists of a parameter name, an equals sign (=), and parameter value inside a quote. Following table shows the above syntax in detail −

Parameter Parameter_value Parameter_description
Version 1.0 Specifies the version of the XML standard used.
Encoding UTF-8, UTF-16, ISO-10646-UCS-2, ISO-10646-UCS-4, ISO-8859-1 to ISO-8859-9, ISO-2022-JP, Shift_JIS, EUC-JP It defines the character encoding used in the document. UTF-8 is the default encoding used.
Standalone yes or no It informs the parser whether the document relies on the information from an external source, such as external document type definition (DTD), for its content. The default value is set to no. Setting it to yes tells the processor there are no external declarations required for parsing the document.

Rules

An XML declaration should abide with the following rules −

  • If the XML declaration is present in the XML, it must be placed as the first line in the XML document.

  • If the XML declaration is included, it must contain version number attribute.

  • The Parameter names and values are case-sensitive.

  • The names are always in lower case.

  • The order of placing the parameters is important. The correct order is: version, encoding and standalone.

  • Either single or double quotes may be used.

  • The XML declaration has no closing tag i.e. </?xml>

XML Declaration Examples

Following are few examples of XML declarations −

XML declaration with no parameters −

<?xml >

XML declaration with version definition −

<?xml version = "1.0">

XML declaration with all parameters defined −

<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>

XML declaration with all parameters defined in single quotes −

<?xml version = '1.0' encoding = 'iso-8859-1' standalone = 'no' ?>
Advertisements