webForumDet fria alternativet

XML-formatering

2 svar · 489 visningar · startad av xcalibrate

xcalibrateMedlem sedan aug. 2002138 inlägg
#1

Hej!

Bör man skriva såhär:
<Personer>
<Person Id="1">
<Namn>Anders</Namn>
<Alder>18</Alder>
</Person>
<Person Id="2">
<Namn>Peter</Namn>
<Alder>22</Alder>
</Person>
</Personer>
Eller:
<Personer>
<Person Id="1" Namn="Anders" Alder="18" />
<Person Id="2" Namn="Peter" Alder="22" />
</Personer>

Fördelar/nackdelar?

UlfTMedlem sedan maj 20016 050 inlägg
#2

Fördelen med den första varianten, är att du kan ha flera namn och åldrar på samma person, om du nu skulle vilja det. Fördelen med den andra varianten, är att den tar mindre plats. Välj vad du vill utifrån vilket behov du har.

CompusaMedlem sedan jan. 20022 952 inlägg
#3

Undrade över något liknande själv förrut och jag lästa följande på w3schools...

w3schools skrev:

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

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.

Don't end up like this (this is not how XML should be:
<note day="12" month="11" year="2002"
to="Tove" from="Jani" heading="Reminder" 
body="Don't forget me this weekend!">
</note>

Det finns lite undantag till dessa regler så jag skulle rekomendera dig att läsa alltihop här: http://www.w3schools.com/xml/xml_attributes.asp

Hur som helst så tycker jag att ditt första exempel är det mest korrekta!

Genererad på 381 ms · cache AV · v20260730165559-full.f96bc7eb