XML technical m...login
XML technical manual
author:php.cn  update time:2022-04-14 15:57:53

XML attributes



XML elements have attributes, similar to HTML.

Attributes provide additional information about elements.


XML Attributes

In HTML, attributes provide additional information about an element:

<img src="../style/images /computer.gif">
<a href="demo.html">

Attributes often provide information that is not part of the data. In the following example, the file type has nothing to do with the data, but is important to the software that needs to handle this element:

<file type="gif">computer.gif</ file>


XML attributes must be quoted

Attribute values ​​must be surrounded by quotes, but both single and double quotes can be used. For example, for a person's gender, the person element can be written like this:

<person sex="female">

Or this can also be written:

<person sex='female'>

If the attribute value itself contains double quotes, you can use single quotes, like this example:

<gangster name='George "Shotgun" Ziegler'>

Or you can use character entities:

<gangster name="George "Shotgun" ; Ziegler">


##XML Elements vs. Attributes

See these examples:

<person sex= "female">
​ <firstname>Anna</firstname>
​ <lastname>Smith</lastname>
</person>

##<person>
​ <sex>female</sex>
​ <firstname>Anna</firstname>
​ <lastname>Smith</lastname>
</person>

In the first instance, sex is an attribute. In the second instance, sex is an element. Both instances provide the same information.

There are no rules that tell us when to use attributes and when to use elements. My experience is that in HTML, attributes are convenient to use, but in XML, you should try to avoid using attributes. If information feels a lot like data, use elements.


My favorite way

The following three XML documents contain exactly the same information:

The date attribute is used in the first example:

<note date="10/01/2008">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

The date element is used in the second example:

<note>
<date>10/01/2008</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

The third example uses an extended date element (this is mine Favorite):

<note>
<date>
​ <day>10</day>
​ <month>01</month>
​ <year>2008</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


##Avoid XML attributes?

Some problems caused by using attributes:

  • Attributes cannot contain multiple values ​​(elements can)

  • Attributes Cannot contain a tree structure (elements can)

  • Attributes are not easily extensible (for future changes)

Attributes are difficult to read and maintain. Please try to use elements to describe data. Instead, just use attributes to provide data-independent information.

Don't do something stupid like this (this is not the way XML should be used):

<note day="10" month="01" year="2008"
to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>


XML attributes for metadata

Sometimes ID references are assigned to elements. These ID indexes can be used to identify XML elements in the same way as the id attribute in HTML. This example demonstrates this to us:

<messages>
​ <note id="501">
​​ <to>Tove</to>
​​ <from>Jani</from>
​​ <heading>Reminder</heading>
​​ <body>Don't forget me this weekend!</body>
​ </note>
​ <note id="502">
​​ <to>Jani</to>
​​ <from>Tove</from>
​​ <heading>Re: Reminder</heading>
​​ <body>I will not</body>
​ </note>
</messages>

The above id attribute is just an identifier, used to identify different notes. It is not part of the note data.

The idea we are trying to convey to you here is that metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.