XML attributes
- Föregående sida XML elements
- Nästa sida XML namespaces
XML-element kan innehålla egenskaper i öppningsetiketten, liknande HTML.
Egenskaper (Attribute) tillhandahåller ytterligare (kompletterande) information om elementen.
XML-egenskaper måste innehålla citattecken
Egenskapsvärden måste omges av citattecken, men både enkla och dubbla citattecken kan användas.
Till exempel en persons kön, kan <person>-taggen skrivas så här:
<person gender="female">
Eller lika bra kan det vara:
<person gender='female'>
Om egenskapsvärdet själv innehållerDubbelcitat, kan du använda enkelcitat, som visas i följande exempel:
<gangster name='George "Shotgun" Ziegler'>
Eller du kan använda karaktersubstitut:
<gangster name="George "Shotgun" Ziegler">
XML-element och egenskaper
Se dessa två exempel:
<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 feels like data, then use child elements.
My favorite way
The following three XML documents contain completely the same information:
In the first example, a date attribute is used:
<note date="2008-01-10"> <to>George</to> <from>John</from> </note>
In the second example, a <date> element is used:
<note> <date>2008-01-10</date> <to>George</to> <from>John</from> </note>
In the third example, an extended date element is 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. And only use attributes to provide information unrelated to the data.
Don't 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="Don't forget the meeting!" </note>
XML attributes for metadata
I sometimes assign IDs to elements. These ID indices can be used to identify XML elements, which works 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>Don't forget the meeting!</body> </note> <note id="502"> <to>John</to> <from>George</from> <heading>Re: Reminder</heading> <body>I will not</body> </note> </messages>
Ovanstående ID är endast en identifierare för att beteckna olika anteckningar. Det är inte en del av anteckningsdata.
Här vill vi starkt kommunicera till dig att metadata (data om data) bör lagras som egenskaper, medan data själva bör lagras som element.
- Föregående sida XML elements
- Nästa sida XML namespaces