XML Guide--xml Properties

Source: Internet
Author: User
Tags date xml attribute
Xml
Like HTML, an XML element can be started with an element attribute at the beginning of the tag.
Properties usually contain additional information about the element.



XML attribute
XML elements can have attributes.
You must remember this HTML code: . SRC is a property of an IMG element that provides additional information about the IMG element.
The attributes of an element provide additional information about the element in HTML (as is also the case in XML).

<a href= "demo.asp" >


The information provided by the letter is not usually part of the data. In the following example, the type and data are irrelevant, but important for the software that operates the element.
<file type= "gif" >http://www.163design.net/x/a/computer.gif</file>





Quote Style "female" or ' female '?
The property value must be enclosed in quotation marks. Single or double quotes are available. For example, one's gender, the person element can be written like this:
<person sex= "female" >


You can also write this:
<person sex= ' female ' >


The above two types of writing in general are no different, the use of double quotes is more common. However, in some special cases, you must use single quotes, such as the following example:
<gangster name= ' George ' shotgun ' Ziegler ' >




Using child elements or using attributes
Data can be stored either in child elements or in attributes.
Take a look at the following example:
<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 a property, and in the second example, sex is a child element. All two examples provide the same information.
When to use attributes and when to use child elements without a ready-made rule can be followed. My experience is that attributes can be quite handy in HTML, but in XML you'd better avoid using them.



I like the style
I prefer to store the data in child elements.
The following three XML documents contain the same information:
The first example uses the Data property:
<note date= "12/11/99" >
<to>Tove</to>
<from>Jani</from>
<body>don ' t forget me this weekend!</body>
</note>


The second example uses the data element:
<note>
<date>12/11/99</date>
<to>Tove</to>
<from>Jani</from>
<body>don ' t forget me this weekend!</body>
</note>


The third example uses the extended Data element: (This is the style I recommend):
<note>
<date>
<day>12</day>
<month>11</month>
<year>99</year>
</date>
<to>Tove</to>
<from>Jani</from>
<body>don ' t forget me this weekend!</body>
</note>





Avoid using attributes?
Should you avoid using attributes?
Here are some questions that are raised by using attributes:
A property cannot contain more than one value (child elements can).
Properties are not easily extensible.
Properties cannot describe structs (child elements can).
Attributes are difficult to be processed by program code.
Attribute values are difficult to test with DTDs.
If you use attributes to store data, the XML document you write must be difficult to read and manipulate. Use elements to describe the data as much as possible, using attributes only to describe the extra information that is not related to the data.
Don't be like the example below (if you do that you don't understand the point above).
<note day= "A" month= "one" year= "99"
To= "Tove" from= "Jani" heading= "Reminder"
body= "Don ' t forget me this weekend!" >
</note>





Exceptions to property 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 refer to accessing a particular XML element, just like the name and id attribute in HTML. Take a look at the following example:
<messages>
<note id= "501" >
<to>Tove</to>
<from>Jani</from>
<body>don ' t forget me this weekend!</body>
</note>
<note id= "502" >
<to>Jani</to>
<from>Tove</from>
<body>i would not!</body>
</note>
</messages>


In the example above, the id attribute is the equivalent of a counter, or a unique identifier that identifies different note messages in an XML document, and he is not part of the note information.
What we are trying to tell our readers is that metadata (data related to 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.