Basic XML development-code explanation of XML attributes

Source: Internet
Author: User
Tags xml attribute
XML elements can contain attributes in the start tag, similar to HTML. Attribute provides additional information about elements. You can recall the XML attribute from HTML: <imgsrc"computer.gif & quot; & gt ;. The & quot; src & quot; attribute provides additional information about the & lt; img & gt; element. In HTML (and XML), attributes provide additional information about elements: XML elements can contain attributes in the start tag, similar to HTML.

Attribute provides additional information about elements.

XML attributes

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

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

 

Attributes usually provide information that is not part of the data. In the following example, the file type has nothing to do with data, but it is very important for the software that needs to process this element:

 
  computer.gif
 

The XML attribute must be enclosed in quotation marks.

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:

 

Alternatively, you can:

 

Note: If the attribute value itself contains double quotation marks, it is necessary to enclose it with single quotation marks, as in this example:

 

Alternatively, you can use entity reference:

 

XML element vs. attributes

See these examples:

 
  
   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 to tell us when to use attributes and when to use subelements. 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 method

The following three XML documents contain identical information:

The date attribute is used in the first example:

 
  
   George
  
  
   John
  
  
   Reminder
  Don't forget the meeting this weekend!
 

In the second example, the date element is used:

 
  
   08/08/2008
  
  
   George
  
  
   John
  
  
   Reminder
  Don't forget the meeting this weekend!
 

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

 
  
   
    08
   
   
    08
   
   
    2008
   
  
  
   George
  
  
   John
  
  
   Reminder
  Don't forget the meeting this weekend!
 

Avoid XML attributes?

Some problems caused by Attribute usage:

Attribute cannot contain multiple values (sub-elements are acceptable)

The property cannot describe the tree structure (the child element can)

Attributes are not easy to expand (for future changes)

Difficult to read and maintain attributes

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 ):

 

XML attributes for metadata

Sometimes an ID reference is assigned to an element. These ID indexes can be used to identify XML elements in the same way as the ID attribute in HTML. This example demonstrates this situation:

 
  
   
    George
   
   
    John
   
   
    Reminder
   Don't forget the meeting this weekend!
  
  
   
    John
   
   
    George
   
   
    Re: Reminder
   I will not
  
 

The above ID is just an identifier used to identify different tabs. It is not part of the signature data.

Here we try to pass you the idea that metadata (data related to data) should be stored as attributes, and data itself should be stored as elements.

The above is a detailed explanation of the code for the XML property-XML development basics. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.