New XML Beginner Tutorial: Learn about learning XML attributes

Source: Internet
Author: User
Tags date contains xml attribute
xml| Tutorials | Getting Started

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

property is used to provide additional information about the element.

XML attribute

XML elements can have attributes.

Recall the HTML tag: . The SRC attribute provides additional information about the IMG element.

In HTML (and XML), an attribute provides additional (additional) information about an element:

<a href= "demo.asp" >

Properties often provide information that is not part of the data. In the following example, the file type and data are irrelevant, but important for the software that needs to be processed:

<file type= "gif" >http://www.webjx.com/htmldata/2007-06-20/computer.gif</file>

Quote type, "female" or ' female '?

Property values must be surrounded by quotation marks, but both single and double quotes are available. such as the sex of someone, the person tag can write:

<person sex= "female" >

Or this can also be:

<person sex= ' female ' >

Note: If the property value itself contains double quotes, it is necessary to surround it with single quotes, like this example:

<gangster name= ' George ' shotgun ' Ziegler ' >

Note: If the property value itself contains single quotes, it is necessary to surround it with double quotes, like this example:

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

Use element or attribute

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

Take a look at 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. All two examples can provide the same information.

There are no rules to tell us when to use attributes and when to use child elements. My experience is that in HTML, attributes are convenient to use, but in XML you should try to avoid using attributes. If the information feels like data, use a child element.

The way I like it the most

I like to store data in child elements.

Here are three XML documents that contain the same information:

The first example uses the attribute:

<note date= "12/11/2002" ><to>Tove</to><from>Jani</from>

The second example uses elements:

<note><date>12/11/2002</date><to>Tove</to><from>Jani</from>< Heading>reminder

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

<note><date>  <day>12</day>  <month>11</month>  

Avoid using attributes?

Should we avoid using attributes?

Some problems caused by the use of attributes:

    • Property cannot contain more than one value (child elements can)
    • Attributes are not easy to expand (for future changes)
    • property cannot describe struct (child element can)
    • Attributes are more difficult to be processed by programming code
    • It is not easy to test property values through a DTD-the DTD is used to define the legitimate elements of the XML document

If you use attributes as a container for data, you can create documents that are difficult to read and maintain. use elements to describe your data as much as possible. Only attributes are used to provide data-independent information.

Don't do such a stupid thing (this is not the way XML should be used):

An exception to a property rule

There are always exceptions to the rules.

Sometimes, I assign an ID index to an element. These ID indices can be used to access XML elements in the same way as the Name property or ID attribute in HTML. This example shows us this situation:

<messages>  <note id= "p501" >    <to>Tove</to>    <from>Jani</from>    

The ID in this example is just a counter, or a unique identifier, used to mark different notes in the XML file, not as part of the sticky note data.

Here we strongly convey to you the idea that metadata (data about data) should be stored as attributes, and that the 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.