Xml 简明教程

XML - Tags

让我们了解 XML 最重要的部分之一,即 XML 标记。 XML tags 构成了 XML 的基础。它们定义了 XML 中元素的作用域。它们还可以用于插入注释、声明解析环境所需的设置,以及插入特殊指令。

Let us learn about one of the most important part of XML, the XML tags. XML tags form the foundation of XML. They define the scope of an element in XML. They can also be used to insert comments, declare settings required for parsing the environment, and to insert special instructions.

我们可以将 XML 标记大体分类如下 −

We can broadly categorize XML tags as follows −

Start Tag

每个非空 XML 元素的起始处都标记有开始标记。以下是开始标记示例 −

The beginning of every non-empty XML element is marked by a start-tag. Following is an example of start-tag −

<address>

End Tag

每个有开始标记的元素都应以结束标记结尾。以下是结束标记示例 −

Every element that has a start tag should end with an end-tag. Following is an example of end-tag −

</address>

请注意,结束标记在元素名称之前包含一个斜杠 ("/")。

Note, that the end tags include a solidus ("/") before the name of an element.

Empty Tag

出现在开始标记和结束标记之间的文本被称为内容。没有内容的元素称为空元素。可以使用以下两种方式来表示空元素 −

The text that appears between start-tag and end-tag is called content. An element which has no content is termed as empty. An empty element can be represented in two ways as follows −

开始标记后立即跟着一个结束标记,如下所示 −

A start-tag immediately followed by an end-tag as shown below −

<hr></hr>

一个完整的空元素标记如下所示 −

A complete empty-element tag is as shown below −

<hr />

对于没有任何内容的任何元素,都可以使用空元素标记。

Empty-element tags may be used for any element which has no content.

XML Tags Rules

以下是使用 XML 标记时需要遵循的规则 −

Following are the rules that need to be followed to use XML tags −

Rule 1

XML 标记区分大小写。下面的代码行是错误语法的示例 </Address>,因为两个标记存在字母大小写差异,在 XML 中被视为错误语法。

XML tags are case-sensitive. Following line of code is an example of wrong syntax </Address>, because of the case difference in two tags, which is treated as erroneous syntax in XML.

<address>This is wrong syntax</Address>

以下代码显示了正确的方式,其中我们使用相同的大小写来命名开始标记和结束标记。

Following code shows a correct way, where we use the same case to name the start and the end tag.

<address>This is correct syntax</address>

Rule 2

必须按适当的顺序关闭 XML 标记,即,在另一元素内打开的 XML 标记必须在外层元素关闭之前关闭。例如 −

XML tags must be closed in an appropriate order, i.e., an XML tag opened inside another element must be closed before the outer element is closed. For example −

<outer_element>
   <internal_element>
      This tag is closed before the outer_element
   </internal_element>
</outer_element>