Xml 简明教程

XML - Declaration

本章详细介绍了 XML 声明。 XML declaration 包含为 XML 处理器解析 XML 文档做准备的详细信息。它是可选的,但使用时,它必须出现在 XML 文档的第一行。

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

以下语法显示 XML 声明 -

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

XML 声明应遵循以下规则 -

An XML declaration should abide with the following rules −

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

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

  3. The Parameter names and values are case-sensitive.

  4. The names are always in lower case.

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

  6. Either single or double quotes may be used.

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

XML Declaration Examples

以下是 XML 声明的一些示例 -

Following are few examples of XML declarations −

没有参数的 XML 声明 -

XML declaration with no parameters −

<?xml >

定义版本的 XML 声明 -

XML declaration with version definition −

<?xml version = "1.0">

定义所有参数的 XML 声明 -

XML declaration with all parameters defined −

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

包含以单引号定义所有参数的 XML 声明 −

XML declaration with all parameters defined in single quotes −

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