XML attributes

 

XML elements can contain attributes in the start tag, like HTML.

Property (Attribute) provides additional (additional) information on the elements.

XML attributes

From HTML, you will remember this: <img src = "computer.gif">. "Src" attribute provides additional information about the <img> element.

In HTML (and in XML) attributes provide additional information about elements:

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

Attribute information not typically provide data component. In the following example, the file type regardless of the data, but very important to deal with this element of the software for:

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

XML attributes must be quoted

Attribute values ​​must be surrounded by quotation marks, although single or double quotes can be used. For example, a person's gender, person tag can be written like this:

<person sex="female">

Or this can also:

<person sex='female'>

Note: If the attribute value itself contains double quotation marks, then it is necessary to use single quotes surrounding it, like in this example:

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

Or you can use entity references:

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

XML elements vs. attributes

Consider 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. Two examples can provide the same information.

No rules to tell us when to use attributes, and when to use child elements. My experience is in HTML, the property is very convenient to use them, but in XML, you should try to avoid using property. If the information feels like data, use sub-element of it.

My favorite way

The following three XML documents contain exactly the same information:

Date property using the first example:

<note date="08/08/2008">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note> 

Date element used in the second example:

<note>
<date>08/08/2008</date>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note> 

Use a third example expanded date element (which 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!</body>
</note>

Avoid XML attribute?

Some of the problems caused by the use of attributes:

  • Attributes can not contain multiple values ​​(elements can)
  • Attributes can not describe the tree structure (elements can)
  • Properties can not be extended (for future changes)
  • Property is difficult to read and maintain

Try to use elements to describe data. But only uses attributes to provide data independent information.

Do not do stupid things (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!">
</note>

XML metadata for the property

Sometimes assign ID references to elements. The index ID is used to identify the XML elements, the way it works in HTML ID attribute is the same. 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!</body>
  </note>
  <note id="502">
    <to>John</to>
    <from>George</from>
    <heading>Re: Reminder</heading>
    <body>I will not</body>
  </note> 
</messages>

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

In this concept we are trying to transfer you to: metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.

Reproduced in: https: //www.cnblogs.com/Codenewbie/archive/2013/04/07/3003700.html

Guess you like

Origin blog.csdn.net/weixin_33895516/article/details/93448081