. The "src" attribute provides additional information about the element. In HTML (and in XML), attributes provide additional information about an element:"/> . The "src" attribute provides additional information about the element. In HTML (and in XML), attributes provide additional information about an element:">
Home >Backend Development >XML/RSS Tutorial >XML development basics-detailed code explanation of XML attributes
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: 08c069fd51d2c2d81cee4e0f0ed64316. The "src" attribute provides additional information about the a1f02c36ba31691bcfe87b2722de723b element.
In HTML (and in XML), attributes provide additional information about an element:
<img src="computer.gif"> <a href="demo.asp">
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>
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">
or this way:
<person sex='female'>
Note: If the attribute value itself contains double quotes, it is necessary Surround it with single quotes, like this example:
<gangster name='George "Shotgun" Ziegler'>
Or you can use entity to quote :
<gangster name="George "Shotgun" Ziegler">
XML Elements vs. Attributes
See These examples:
<person sex="female">Anna Smith female Anna Smith
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't forget the meeting this weekend!</body> </note>
The second example uses the date element:
<note> <date>08/08/2008</date> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting this weekend!</body> </note>
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't forget the meeting this weekend!</body> </note>
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't forget the meeting 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>George</to> <from>John</from> <heading>Reminder</heading> <body>Don'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>
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!