ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: XML :: XML ÊôÐÔ

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

XML
XML ½éÉÜ
XML µÄÓÃ;
XML Óï·¨¹æÔò
XML ÔªËØ
XML ÊôÐÔ
XML µÄÓÐЧÐÔÑéÖ¤
XML УÑéÆ÷
Ö§³Ö XML µÄä¯ÀÀÆ..
ä¯ÀÀ XML Îļþ
Óà CSS ÏÔʾ XML
Óà XSL ÏÔʾ XML
XML Êý¾Ýµº
XML ½âÎöÆ÷
ÏÖʵÖÐµÄ XML
XML ÃüÃû¿Õ¼ä
XML CDATA
XML ·þÎñÆ÷
XML Ó¦ÓóÌÐò
XMLHttpRequest ¶..
XML ±£´æÊý¾Ý

XML ÊôÐÔ


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-29 ÐÞ¸Ä:2008-04-05 ä¯ÀÀ:5660 :: ::

XML elements can have attributes in the start tag, just like HTML.
XML ÔªËØ¿ÉÒÔÏñHTMLÄÇÑùÔÚÆðʼ±êÇ©ÄÚÊéдÊôÐÔ¡£

Attributes are used to provide additional information about elements.
ÊôÐÔÊÇÓÃÀ´ÌṩÓëÔªËØÏà¹ØµÄ¸½¼ÓÐÅÏ¢µÄ¡£


XML Attributes
XMLÊôÐÔ

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">
<a href="demo.asp">

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>

Quote Styles, "female" or 'female'?
ÒýºÅÀàÐÍ£¬"female" »¹ÊÇ 'female'?

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">

Use of Elements vs. Attributes
±È½ÏÔªËØºÍÊôÐÔµÄʹÓ÷½·¨

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 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ÖУ¬ÄãÓ¦¸Ã¾¡Á¿±ÜÃâÓÃÊôÐÔ¡£Èç¹ûÐÅÏ¢Öаüº¬´óÁ¿µÄÊý¾Ý£¬ÄÇô¾Í¾¡Á¿Ê¹ÓÃ×ÓÔªËØ¡£


My Favorite Way
ÎÒ×îϲ»¶µÄ·½·¨

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">
<to>Tove</to>
<from>Jani</from>

<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

A date element is used in the second example:
µÚ¶þ¸ö°¸ÀýÖÐʹÓÃÁËÈÕÆÚÔªËØ£º

<note>
<date>12/11/2002</date>

<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

An expanded date element is used in the third: (THIS IS MY FAVORITE):
µÚÈý¸ö°¸ÀýÖÐʹÓÃÁËÀ©Õ¹ÈÕÆÚÔªËØ£¨ÕâÊÇÎÒÃdz£Óõķ½·¨£©£º

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

<year>2002</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>
</note>

Avoid using attributes?
±ÜÃâʹÓÃÊôÐÔ£¿

Should you avoid using attributes?
ÄãÕæµÄÓ¦¸Ã±ÜÃâʹÓÃÊôÐÔÂð£¿

Some of the problems with using attributes are:
ʹÓÃÊôÐÔ»á´øÀ´µÄһЩÎÊÌ⣺

  • attributes cannot contain multiple values (child elements can)
    ÊôÐÔ²»Äܺ¬ÓжàÖØÖµ£¨×ÓÔªËØ¿ÉÒÔ£©
  • attributes are not easily expandable (for future changes)
    ÊôÐÔ²»Ò×À©Õ¹£¨ÕâÀï˵µÄ¿ÉÀ©Õ¹ÐÔÊÇÕë¶Ô½«À´µÄÊôÐԱ仯À´½²µÄ£©
  • attributes cannot describe structures (child elements can)
    ÊôÐÔ²»ÄÜÃèÊö½á¹¹£¨Èç¹ûÊÇ×ÓÔªËØµÄ»°¿ÉÒÔÃèÊö½á¹¹£©
  • attributes are more difficult to manipulate by program code
    ÊôÐÔ²»Ò×±»³ÌÐò´úÂë´¦Àí
  • attribute values are not easy to test against a Document Type Definition (DTD) - which is used to define the legal elements of an XML document
    ÊôÐÔÖµ²»Ò×±»Îı¾ÀàÐͶ¨Ò壨DTD£©²âÊÔ£¬DTDÊÇÓÃÀ´¶¨ÒåXMLÎĵµÖеĺϷ¨ÔªËصÄ

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"

to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>

An Exception to my Attribute Rule
ÎÒµÄÊôÐÔʹÓùæÔòÖ®ÀýÍâ

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>

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

<body>Don't forget me this weekend!</body>
</note>

<note id="p502">
<to>Jani</to>

<from>Tove</from>
<heading>Re: Reminder</heading>
<body>I will not!</body>
</note>

</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£¨ÔªÊý¾Ý£ºÓëÊý¾ÝÏà¹ØµÄÊý¾Ý£©Ó¦¸ÃÒÔÊôÐÔ·½Ê½´¢´æ£¬¶øÊý¾Ý±¾ÉíÓ¦¸ÃÒÔÔªËØ·½Ê½´¢´æ¡£

ÆÀÂÛ (3) 1 All