Title: XML getting started Tutorial: Learn about XML attributes

Source: Internet
Author: User
Title: XML getting started Tutorial: Learn about XML attributes
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 attributes

XML 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 attribute

Data 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 method

I 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 rule

There 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.

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.