XML development basics-detailed code explanation of XML attributes

黄舟
Release: 2017-03-25 17:05:03
Original
1521 people have browsed it

XML elements can contain the attribute in the opening tag, similar to HTML.

Attributes provide additional information about the element.

XML Attributes

From HTML, you will recall this: . The "src" attribute provides additional information about the element.

In HTML (and in XML), attributes provide additional information about an element:

<img src="computer.gif">
<a href="demo.asp">
Copy after login

Attributes often provide information that is not part of the data. In the following example, the file type is irrelevant to the data, but is important to the software that needs to process this element:

<file type="gif">computer.gif</file>
Copy after login

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 tag can be written like this:

<person sex="female">
Copy after login

or this way:

<person sex=&#39;female&#39;>
Copy after login

Note: If the attribute value itself contains double quotes, it is necessary Surround it with single quotes, like this example:

<gangster name=&#39;George "Shotgun" Ziegler&#39;>
Copy after login

Or you can use entity to quote :

<gangster name="George "Shotgun" Ziegler">
Copy after login

XML Elements vs. Attributes

See These examples:

<person sex="female">
Anna
Smith


female
Anna
Smith
Copy after login

In the first example, sex is an attribute. In the second example, sex is a child element. Both examples provide the same information.

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

My favorite way

The following three XML documents contain the exact same information:

The first example uses the date attribute :

<note date="08/08/2008">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don&#39;t forget the meeting this weekend!</body>
</note>
Copy after login

The second example uses the date element:

<note>
<date>08/08/2008</date>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don&#39;t forget the meeting this weekend!</body>
</note>
Copy after login

The third example uses the extended date element (this is my favorite):

<note>
<date>
<day>08</day>
<month>08</month>
<year>2008</year>
</date>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don&#39;t forget the meeting this weekend!</body>
</note>
Copy after login

Avoid XML attributes?

Some problems caused by using attributes:

Attributes cannot contain multiple values ​​(subelements can)

Attributes cannot describe the tree structure (subelements can)

Attributes are not easy to extend (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="08" month="08" year="2008"
to="George" from="John" heading="Reminder"
body="Don&#39;t forget the meeting this weekend!">
</note>
Copy after login

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>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don&#39;t forget the meeting this weekend!</body>
</note>
<note id="502">
<to>John</to>
<from>George</from>
<heading>Re: Reminder</heading>
<body>I will not</body>
</note>
</messages>
Copy after login

The ID above 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.

The above is the detailed content of XML development basics-detailed code explanation of XML attributes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!