Xml 简明教程
XML - Elements
XML elements 可以定义为 XML 的构建基块。元素可以充当容器来容纳文本、元素、属性、媒体对象或所有这些。
XML elements can be defined as building blocks of an XML. Elements can behave as containers to hold text, elements, attributes, media objects or all of these.
每个 XML 文档包含一个或多个元素,其范围由开始和结束标记(或对于空元素,由空元素标记)定界。
Each XML document contains one or more elements, the scope of which are either delimited by start and end tags, or for empty elements, by an empty-element tag.
Syntax
以下是编写 XML 元素的语法 −
Following is the syntax to write an XML element −
<element-name attribute1 attribute2>
....content
</element-name>
其中,
where,
-
element-name is the name of the element. The name its case in the start and end tags must match.
-
attribute1, attribute2 are attributes of the element separated by white spaces. An attribute defines a property of the element. It associates a name with a value, which is a string of characters. An attribute is written as −
name = "value"
名称后面紧跟一个 = 号和一对双引号(“ ”)或单引号(' ')引起来的字符串值。
name is followed by an = sign and a string value inside double(" ") or single(' ') quotes.
Empty Element
一个空元素(没有内容的元素)拥有以下语法:
An empty element (element with no content) has following syntax −
<name attribute1 attribute2.../>
下面是一个使用各种 XML 元素的 XML 文档示例:
Following is an example of an XML document using various XML element −
<?xml version = "1.0"?>
<contact-info>
<address category = "residence">
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</address>
</contact-info>
XML Elements Rules
对于 XML 元素,必须遵守如下规则:
Following rules are required to be followed for XML elements −
-
An element name can contain any alphanumeric characters. The only punctuation mark allowed in names are the hyphen (-), under-score (_) and period (.).
-
Names are case sensitive. For example, Address, address, and ADDRESS are different names.
-
Start and end tags of an element must be identical.
-
An element, which is a container, can contain text or elements as seen in the above example.