Body: Similar to HTML, XML elements can also contain attributes in the start tag. An attribute is used to provide additional information about an element. XML attributesXML elements can have attributes. Recall this HTML Tag: . The src attribute provides additional information about the IMG element. In HTML (and XML), attributes provide additional (additional) Information about elements: <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 it is very important for the software that needs to process this element: <file type="gif">http://www.webjx.com/htmldata/2007-06-20/computer.gif</file> The quotation mark type, "female" or 'female '?Attribute values must be enclosed by quotation marks, but both single quotation marks and double quotation marks can be used. For example, the person tag can be written as follows for a person's gender: <person sex="female"> Alternatively, you can: <person sex='female'> Note: If the attribute value itself contains double quotation marks, it is necessary to enclose it with single quotation marks, as in this example: <gangster name='George "Shotgun" Ziegler'> Note: If the attribute value itself contains single quotes, it is necessary to enclose it with double quotes, as in this example: <gangster name="George 'Shotgun' Ziegler"> Use element or attributeData can be stored in sub-elements or in 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 example, sex is an attribute. In the second example, sex is a child element. Both examples provide the same information. There are no rules to tell us when to use the attribute and when to use the child element. In my experience, attributes are easy to use in HTML, but in XML, you should try to avoid using attributes. If the information is similar to data, use the child element. My favorite methodI like to store data in sub-elements. The following are three XML documents containing the same information: In the first example, attributes are used: <note date="12/11/2002"><to>Tove</to><from>Jani</from>In the second example, elements are used: <note><date>12/11/2002</date><to>Tove</to><from>Jani</from>The third example uses extended elements (my favorite ): <note><date> <day>12</day> <month>11</month> <year>2002</year></date><to>Tove</to><from>Jani</from>Avoid using attributes?Should we avoid using attributes? Some problems caused by attribute usage:
- Attribute cannot contain multiple values (sub-elements are acceptable)
- Attributes are not easy to expand (for future changes)
- The attribute cannot describe the structure (the child element can)
- Attributes are more difficult to be processed by programming code.
- It is not easy to use a DTD to test the attribute value-DTD is used to define the legal elements of an XML document
If you use attributes as the data container, it will make it difficult to read and maintain documents.Try to use elements to describe data. Only attributes are used to provide data-independent information. Do not do this stupid thing (this is not the way XML should be used ): <note day="12" month="11" year="2002"to="Tove" from="Jani" heading="Reminder" body="Don't forget me this weekend!"></note> An exception to an attribute ruleThere are always exceptions to rules. Sometimes, I will assign an ID index to the element. These ID indexes can be used to access XML elements in the same way as the name or ID attribute in HTML. This example demonstrates this situation: <messages> <note id="p501"> <to>Tove</to> <from>Jani</from> In this example, the ID is only a counter or a unique identifier used to indicate different tabs in the XML file. It is not part of the tag data. Here we try to convey the following ideas to you:Metadata (data related to data) should be stored as attributes, and data itself should be stored as elements. |