Muundo wa kipengele za XML

XML 元素可以在开始标签中包含属性,类似 HTML。

属性 (Attribute) 提供关于元素的额外(附加)信息。

Kipimo cha XML inahitajika kuwa kijani

Thamani ya kipimo inahitajika kuwa kijani, herufi kibiri na kikibiri zinaaminika.

Kama mfano wa jinsia ya mtu, tabia ya <person> inayoitwa kama:

<person gender="female">

ama hiki pia inaweza:

<person gender='female'>

Ikiwa thamani ya uharibifu ina herufi kipya, inaweza kutumia herufi kibiri, kama inaonyeshwa kwanza:

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

auya, unaweza kutumia mafanikio ya herufi:

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

Mfano wa XML na ujumbe wa kipimo

Tazama mifano hizi:

<person gender="female">
  <firstname>Anna</firstname>
  <lastname>Smith</lastname>
</person>
<person>
  <gender>female</gender>
  <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 can provide the same information.

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

My favorite way

The following three XML documents contain completely the same information:

In the first example, date was used as an attribute:

<note date="2008-01-10">
  <to>George</to>
  <from>John</from>
</note>

In the second example, the <date> element was used:

<note>
  <date>2008-01-10</date>
  <to>George</to>
  <from>John</from>
</note>

In the third example, an extended date element was used (this is my favorite):

<note>
  <date>
    <year>2008</year>
    <month>01</month>
    <day>10</day>
  </date>
  <to>George</to>
  <from>John</from>
</note>

Avoid using XML attributes?

Some considerations when using attributes are:

  • Attributes cannot contain multiple values (elements can)
  • Attributes cannot describe tree structures (elements can)
  • Attributes are not easy to extend (for future changes)

Try to use elements to describe data. Use attributes only to provide information unrelated to data.

Do not do such foolish things (this is not the way XML should be used):

<note day="10" month="01" year="2008"
to="George" from="John" heading="Reminder"
body="Do not forget the meeting!">
</note>

XML attributes for metadata

Sometimes, IDs are assigned to elements for reference. These ID indices can be used to identify XML elements, working in the same way as the ID attribute in HTML. This example demonstrates this situation:

<messages>
  <note id="501">
    <to>George</to>
    <from>John</from>
    <heading>Reminder</heading>
    <body>Do not forget the meeting!</body>
  </note>
  <note id="502">
    <to>John</to>
    <from>George</from>
    <heading>Re: Mwembelee</heading>
    <body>angu hakuna</body>
  </note> 
</messages>

ID yenye kina yake yana uwanja wa kina tu, una uwanja wa kina wa kina wa tabia. Huwa si kina ya data ya tabia.

Hapa tunahatia kwa kuzingatia kwamba metadata (data ya data) lazima iweze kuhifadhiwa kama mafuta, na data yoyote lazima iweze kuhifadhiwa kama maelezo.