Xml 简明教程
XML - Attributes
这一章描述了 XML attributes 。属性是 XML 元素的一部分。一个元素可以具有多个唯一的属性。属性提供了有关 XML 元素的更多信息。更准确地说,它们定义了元素的属性。XML 属性始终是名称-值对。
This chapter describes the XML attributes. Attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements. An XML attribute is always a name-value pair.
Syntax
XML 属性具有以下语法 −
An XML attribute has the following syntax −
<element-name attribute1 attribute2 >
....content..
< /element-name>
其中 attribute1 和 attribute2 具有以下形式 −
where attribute1 and attribute2 has the following form −
name = "value"
value 必须位于双引号 (“ ") 或单引号 (' ') 中。此处,attribute1 和 attribute2 是唯一的属性标签。
value has to be in double (" ") or single (' ') quotes. Here, attribute1 and attribute2 are unique attribute labels.
属性用于向元素添加唯一的标签,将标签置于一个类别中,添加布尔标志,或将它与某些数据字符串关联。以下示例演示了属性的使用 −
Attributes are used to add a unique label to an element, place the label in a category, add a Boolean flag, or otherwise associate it with some string of data. Following example demonstrates the use of attributes −
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE garden [
<!ELEMENT garden (plants)*>
<!ELEMENT plants (#PCDATA)>
<!ATTLIST plants category CDATA #REQUIRED>
]>
<garden>
<plants category = "flowers" />
<plants category = "shrubs">
</plants>
</garden>
属性用于区分同名元素,当您不想为每种情况都创建一个新元素时。因此,使用属性可以在区分两个或多个相似元素时添加更多细节。
Attributes are used to distinguish among elements of the same name, when you do not want to create a new element for every situation. Hence, the use of an attribute can add a little more detail in differentiating two or more similar elements.
在以上示例中,我们通过包括属性类别并将不同的值分配给每个元素来对植物进行分类。因此,我们有两个类别的植物,一类是花卉,另一类是灌木。因此,我们有两个带有不同属性的植物元素。
In the above example, we have categorized the plants by including attribute category and assigning different values to each of the elements. Hence, we have two categories of plants, one flowers and other shrubs. Thus, we have two plant elements with different attributes.
您还可以观察到我们在 XML 的开头声明了此属性。
You can also observe that we have declared this attribute at the beginning of XML.
Attribute Types
下表列出了属性类型 −
Following table lists the type of attributes −
Attribute Type |
Description |
StringType |
It takes any literal string as a value. CDATA is a StringType. CDATA is character data. This means, any string of non-markup characters is a legal part of the attribute. |
TokenizedType |
This is a more constrained type. The validity constraints noted in the grammar are applied after the attribute value is normalized. The TokenizedType attributes are given as − ID − It is used to specify the element as unique. IDREF − It is used to reference an ID that has been named for another element. IDREFS − It is used to reference all IDs of an element. ENTITY − It indicates that the attribute will represent an external entity in the document. ENTITIES − It indicates that the attribute will represent external entities in the document. NMTOKEN − It is similar to CDATA with restrictions on what data can be part of the attribute. NMTOKENS − It is similar to CDATA with restrictions on what data can be part of the attribute. |
EnumeratedType |
This has a list of predefined values in its declaration. out of which, it must assign one value. There are two types of enumerated attribute − NotationType − It declares that an element will be referenced to a NOTATION declared somewhere else in the XML document. Enumeration − Enumeration allows you to define a specific list of values that the attribute value must match. |
Element Attribute Rules
以下是针对属性需要遵循的规则 −
Following are the rules that need to be followed for attributes −
-
An attribute name must not appear more than once in the same start-tag or empty-element tag.
-
An attribute must be declared in the Document Type Definition (DTD) using an Attribute-List Declaration.
-
Attribute values must not contain direct or indirect entity references to external entities.
-
The replacement text of any entity referred to directly or indirectly in an attribute value must not contain a less than sign (<)