w3pop.com :: ÍøÂçѧԺ :: XML :: XML ÊôÐÔ
XML elements can have attributes in the start tag, just like HTML.
XML ÔªËØ¿ÉÒÔÏñHTMLÄÇÑùÔÚÆðʼ±êÇ©ÄÚÊéдÊôÐÔ¡£
Attributes are used to provide additional information about elements.
ÊôÐÔÊÇÓÃÀ´ÌṩÓëÔªËØÏà¹ØµÄ¸½¼ÓÐÅÏ¢µÄ¡£
XML elements can have attributes.
XML ÔªËØ¿ÉÒÔÓÐÊôÐÔ¡£
From HTML you will remember this: <IMG SRC="computer.gif">. The SRC attribute provides additional information about the IMG element.
ÔÚHTML½Ì³ÌÖУ¬ÄãÒ»¶¨»¹¼ÇµÃÕâ¸ö£º<IMG SRC="computer.gif">£¬ SRC ÊôÐÔÌṩÁËÓë IMG ÔªËØÏà¹ØµÄ¸½¼ÓÐÅÏ¢¡£
In HTML (and in XML) attributes provide additional information about elements:
ÔÚHTML£¨ÒÔ¼°XML£©ÖУ¬ÊôÐÔÊÇÌṩÓëÔªËØÏà¹ØµÄ¸½¼ÓÐÅÏ¢µÄ¡£
<img src="computer.gif"> |
Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:
ÊôÐÔͨ³£Ìṩ³ýÊý¾Ý²¿·ÖÒÔÍâµÄÐÅÏ¢£¬ÔÚÏÂÃæµÄ°¸ÀýÖУ¬ÎļþÀàÐÍÓëÊý¾Ý²»Ïà¹Ø£¬µ«¶Ô´¦ÀíÔªËØµÄÈí¼þÈ´ºÜÖØÒª£º
<file type="gif">computer.gif</file> |
Attribute values must always be enclosed in quotes, but either single or double quotes can be used. For a person's sex, the person tag can be written like this:
ÊôÐÔÖµ±ØÐëÊéдÔÚÒýºÅÀµ¥ÒýºÍË«Òý¶¼¿ÉÒÔÓá£ÒÔijÈËÐÔ±ðΪÀý£¬ÈËÎï±êÇ©¿ÉÒÔÕâÑùÊéд£º
<person sex="female"> |
or like this:
»òÕßÕâÑù
<person sex='female'> |
Note: If the attribute value itself contains double quotes it is necessary to use single quotes, like in this example:
×¢Òâ: Èç¹ûÊôÐÔÖµ±¾Éí°üº¬Ë«ÒýºÅ£¬ÄÇÒýÓÃÕû¾äʱ¾Í±ØÐëʹÓõ¥ÒýºÅ£¬¼ûÏÂÃæµÄ°¸Àý£º
<gangster name='George "Shotgun" Ziegler'> |
Note: If the attribute value itself contains single quotes it is necessary to use double quotes, like in this example:
×¢Òâ: Èç¹ûÊôÐÔÖµ±¾Éí°üº¬µ¥ÒýºÅ£¬ÄÇÒýÓÃÕû¾äʱ¾Í±ØÐëʹÓÃË«ÒýºÅ£¬¼ûÏÂÃæµÄ°¸Àý£º
<gangster name="George 'Shotgun' Ziegler"> |
Data can be stored in child elements or in attributes.
Êý¾Ý¿ÉÒÔ´æ´¢ÔÚ×ÓÔªËØ»òÊôÐÔÖС£
Take a look at these examples:
Çë¿´ÏÂÃæÕâЩ°¸Àý£º
<person sex="female"> |
<person> |
In the first example sex is an attribute. In the last, sex is a child element. Both examples provide the same information.
ÔÚµÚÒ»¸ö°¸ÀýÖУ¬ÐÔ±ðÊÇÒ»¸öÊôÐÔ¡£ÔÚ×îºóµÄ°¸ÀýÖУ¬ÐÔ±ðÊÇÒ»¸ö×ÓÔªËØ¡£Á½¸ö°¸ÀýÌṩµÄÐÅÏ¢Ò»Ñù¡£
There are no rules about when to use attributes, and when to use child elements. My experience is that attributes are handy in HTML, but in XML you should try to avoid them. Use child elements if the information feels like data.
ÖÁÓÚºÎʱʹÓÃÊôÐÔ»òºÎʱʹÓÃ×ÓÔªËØ²¢Ã»ÓоßÌåµÄ¹æ¶¨¡£ÎҵľÑéÊÇ£º¶ÔÓÚHTMLÖеÄÊôÐÔÀ´Ëµ£¬²Ù×÷ÆðÀ´±È½ÏÈÝÒ×£¬µ«ÔÚXMLÖУ¬ÄãÓ¦¸Ã¾¡Á¿±ÜÃâÓÃÊôÐÔ¡£Èç¹ûÐÅÏ¢Öаüº¬´óÁ¿µÄÊý¾Ý£¬ÄÇô¾Í¾¡Á¿Ê¹ÓÃ×ÓÔªËØ¡£
I like to store data in child elements.
ÎÒϲ»¶½«Êý¾Ý´æ´¢ÔÚ×ÓÔªËØÖС£
The following three XML documents contain exactly the same information:
ÒÔÏÂÈý·ÝXMLÎĵµ¶¼°üº¬ÏàͬµÄÐÅÏ¢£º
A date attribute is used in the first example:
µÚÒ»¸ö°¸ÀýÖÐʹÓÃÁËÈÕÆÚÊôÐÔ£º
<note date="12/11/2002"> |
A date element is used in the second example:
µÚ¶þ¸ö°¸ÀýÖÐʹÓÃÁËÈÕÆÚÔªËØ£º
<note> |
An expanded date element is used in the third: (THIS IS MY FAVORITE):
µÚÈý¸ö°¸ÀýÖÐʹÓÃÁËÀ©Õ¹ÈÕÆÚÔªËØ£¨ÕâÊÇÎÒÃdz£Óõķ½·¨£©£º
<note> |
Should you avoid using attributes?
ÄãÕæµÄÓ¦¸Ã±ÜÃâʹÓÃÊôÐÔÂð£¿
Some of the problems with using attributes are:
ʹÓÃÊôÐÔ»á´øÀ´µÄһЩÎÊÌ⣺
If you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data.
Èç¹ûÄãʹÓÃÊôÐÔ×÷Ϊ´¢´æÊý¾ÝµÄÈÝÆ÷£¬ÄÇô×îÖÕÉú³ÉµÄÎļþ½«²»Ò×ÓÚÔĶÁºÍ³¤Ê±¼ä³ÖÐøµØÊ¹Óá£ÊÔ×ÅʹÓÃÔªËØ¶ÔÊý¾Ý½øÐÐÃèÊö£»½öÔÚÌṩһЩÓëÊý¾ÝÎ޹صÄÐÅÏ¢ÊDzÅʹÓÃÊôÐÔ¡£
Don't end up like this (this is not how XML should be used):
²»ÒªÕâÑù½á⣨XML²»Ó¦¸ÃÕâÑùʹÓã©£º
<note day="12" month="11" year="2002" |
Rules always have exceptions.
¹æÔò³£ÓÐÀýÍâ¡£
My rule about attributes has one exception:
ÎÒµÄÊôÐÔʹÓùæÔòÓÐÒ»¸öÀýÍâ¡£
Sometimes I assign ID references to elements. These ID references can be used to access XML elements in much the same way as the NAME or ID attributes in HTML. This example demonstrates this:
ÓÐʱÎÒ°ÑID²ÎÊý¸³Öµ¸øÔªËØ¡£ÕâЩID²ÎÊý¿ÉÓÃÀ´·ÃÎÊXMLÔªËØ£¬ÕâÓëHTMLÖÐµÄ NAME »ò ID ÊôÐÔ´æÈ¡XMLÔªËØµÄ·½Ê½´óÖÂÏàͬ¡£ÒÔϵݸÀý¿ÉÒÔ˵Ã÷ÎÊÌ⣺
<messages> |
The ID in these examples is just a counter, or a unique identifier, to identify the different notes in the XML file, and not a part of the note data.
ÕâЩÀý×ÓÖÐIDÖ»ÊÇÒ»¸ö“¼ÆÊýÆ÷”£¬»òÕß¿ÉÒÔ³ÆÖ®ÎªÒ»¸öÌØ±ðµÄ“ʶ±ðÆ÷”£¬ÓÃÓÚʶ±ðXMLÎļþÖв»Í¬µÄnoteÐÅÏ¢£¬µ«ËüÃDz»ÊÇnoteÊý¾ÝµÄÒ»²¿·Ö¡£
What I am trying to say here is that metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.
ÎÒÏë×ÅÖØÇ¿µ÷µÄÊÇ£¬metadata£¨ÔªÊý¾Ý£ºÓëÊý¾ÝÏà¹ØµÄÊý¾Ý£©Ó¦¸ÃÒÔÊôÐÔ·½Ê½´¢´æ£¬¶øÊý¾Ý±¾ÉíÓ¦¸ÃÒÔÔªËØ·½Ê½´¢´æ¡£