📜  XML-属性

📅  最后修改于: 2020-11-09 05:26:42             🧑  作者: Mango


本章介绍XML属性。属性是XML元素的一部分。一个元素可以具有多个唯一属性。该属性提供有关XML元素的更多信息。更准确地说,它们定义元素的属性。 XML属性始终是名称/值对。

句法

XML属性具有以下语法-


....content..
< /element-name>

其中attribute1attribute2具有以下形式-

name = "value"

必须用双引号(“”)或单引号(”)引起来。在这里, attribute1attribute2是唯一的属性标签。

属性用于将唯一标签添加到元素,将标签放置在类别中,添加布尔标志或将其与某些数据字符串关联。以下示例演示了属性的使用-



   
   
   
]>


   
   
   

当您不想为每种情况创建新元素时,可使用属性来区分同名元素。因此,使用属性可以在区分两个或更多相似元素时增加更多细节。

在上面的示例中,我们通过包括属性类别并对每个元素分配不同的值来对植物进行了分类。因此,我们有两种植物,一种是,另一种是灌木。因此,我们有两个具有不同属性的植物元素。

您还可以观察到我们已经在XML的开头声明了此属性。

属性类型

下表列出了属性的类型-

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.

元素属性规则

以下是属性需要遵循的规则-

  • 属性名称在同一开始标签或空元素标签中不得出现多次。

  • 必须使用“属性列表声明”在文档类型定义(DTD)中声明属性。

  • 属性值不得包含对外部实体的直接或间接实体引用。

  • 在属性值中直接或间接引用的任何实体的替换文本都不得包含小于号( < )