Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of XML attribute

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the sample analysis of XML attributes, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

As with HTML, XML elements can be marked at the beginning by element attributes.

Attributes usually contain some additional information about the element.

XML attribute

XML elements can have attributes.

You must remember this HTML code:

。src is an attribute of the img element that provides additional information about the img element.

In HTML (and in XML as well) the attributes of an element provide additional information about the element.

The information provided by letters is usually not part of the data. In the following example, type and data are irrelevant, but important to the software that operates on the element.

computer.gif

Reference style "female" or "female"?

Attribute values must be enclosed in quotation marks. Single quotes and double quotes can be used. For example, a person's gender, the person element can be written like this:

You can also write:

The above two ways of writing in general is no difference, the use of double quotes is more common. However, there are special circumstances in which single quotes must be used, such as the following example:

Use child elements or attributes

Data can be stored either in child elements or in attributes.

See the following example:

Anna Smith female Anna Smith

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

There is no ready-made rule for when to use attributes and when to use child elements. My experience is that attributes can be quite handy in HTML, but in XML you're better off avoiding them.

I like the style

I prefer to store data in child elements.

The following three XML documents contain the same information:

The first example uses the data attribute:

Tove Jani Reminder Don't forget me this weekend!

The second example uses the data element:

12/11/99 Tove Jani Reminder Don't forget me this weekend!

The third example uses the extended data element: (this is my recommended style):

12 11 99 Tove Jani Reminder Don't forget me this weekend!

Avoid using attributes?

Should attributes be avoided?

Here are some questions raised by using attributes:

Attributes cannot contain multiple values (child elements can).

Properties are not easily extensible.

Attributes cannot describe structure (child elements can).

Attributes are difficult to process by program code.

Attribute values are difficult to test with DTDs.

If you use attributes to store data, you must write XML documents that are difficult to read and manipulate. Use elements to describe data, and attributes only to describe extra information that is not relevant to the data.

Don't follow the example below (if you do, you don't understand the point above).

Exceptions to attribute rules

There are always exceptions to the rules.

There is one exception to my rule about attributes:

Sometimes you should design an ID reference for an element that can be used to access specific XML elements, just like the name and id attributes in HTML. Consider the following example:

Tove Jani Reminder Don't forget me this weekend! Jani Tove Re: Reminder I will not!

In the above example, the ID attribute is equivalent to a counter, or a unique identifier, identifying different note messages in the XML document that are not part of the note message.

What we are trying to show our readers is that metadata (data related to data) should be stored as attributes, and the data itself should be stored as elements.

Thank you for reading this article carefully. I hope that the article "Sample Analysis of XML Attributes" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support it a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report