Home >Backend Development >XML/RSS Tutorial >Detailed introduction to attribute learning methods in XML

Detailed introduction to attribute learning methods in XML

黄舟
黄舟Original
2017-03-10 19:52:041789browse

This article mainly introduces the attribute learning tutorial in XML, including examples of using attributes to store data in sub-elements. Friends in need can refer to it

Attributes are part of XML elements. An element can have multiple unique attributes. Attributes provide more information about an XML element. More precisely, they define the element's properties. An XML attribute is always a name-value pair.

Syntax
The XML attribute syntax is as follows:

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

where attribute1 and attribute2 have the following form:

name = "value"

value must be wrapped in double quotes (" ") or single quotes (' '). Here attribute1 and attribute2 are both unique attribute labels.

Attributes are used to add a unique label to an element, a category label, add a Boolean attribute or associate some string data. The following example demonstrates how to use 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 elements with the same name. When we don't want to create a new element for every case. We can use attributes to add more detail to differentiate between two or more similar elements.

In the above example, we have classified the plants by including the category attribute and assigned a different value to each element. So we have two plants categories, one is flowers and the other is color. This way we both get two plants elements with different attributes.

You can also see that we define this attribute at the beginning of the XML.

Attribute Type
The following table lists the attribute types:

Attribute Type Description
StringType Accepts a string value as the value. CDATA is a StringType. CDATA is also character data. This also means that any non-markup character is a legal attribute.
TokenizedType

This is a restricted type. Validity constraints indicated in the grammar are applied after attribute values ​​are normalized. The following is the TokenizedType attribute:

  • ID: is used to specify that the element is unique.

  • IDREF: is used to reference an ID that names another element.

  • IDREFS: All IDs used to reference an element.

  • ENTITY: Indicates that the attribute will represent an external entity in the document.

  • #ENTITYS: Indicates that the attribute will represent an external entity in the document.

  • NMTOKEN: Similar to CDATA that limits what data can be part of an attribute.

  • NMTOKENS: Similar to CDATA that limits what data can be part of an attribute.

EnumeratedType

Contains a predefined list of values ​​in its declaration. Here it must be assigned a value. There are two types of enumeration attributes:

  • NotationType: It declares that the element will be referenced as a NOTATION statement somewhere in the XML document.

  • Enumeration: Enumerations allow us to define a specific list of values ​​that an attribute value must match.


Element attribute rules
The following are the rules to follow when defining attributes:

The attribute name can only appear once in the same start tag or empty element tag.
Attributes must be defined in the document type definition (DTD) using the Attribute-List Declaration.
Attribute values ​​cannot directly or indirectly reference external entities.
The alternative text of any entity mentioned directly or indirectly in the attribute value cannot contain a less than sign (<).

Storing data in child elements
The date attribute is used in one case:

<note date="12/11/2002">
<to>Tove</to>
<from>Jani</from>

<heading>Reminder</heading>
<body>Don&#39;t forget me this weekend!</body>
</note>

It is used in the second case Date element:

<note>
<date>12/11/2002</date>

<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don&#39;t forget me this weekend!</body>
</note>

The extended date element is used in the third case (this is our common method):

<note>
<date>
  <day>12</day>
  <month>11</month>

  <year>2002</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>

<body>Don&#39;t forget me this weekend!</body>
</note>


The above is the detailed content of Detailed introduction to attribute learning methods in XML. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn