I'm rebuilding some XML feeds, so I am researching when to use elements and when to use attributes with XML.
Several sites have said "Data goes in elements, metadata in attributes."
So, what is the difference between the two?
Let's take an example from W3Schools:
<note date="12/11/2002">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Should the date stay as an attribute of the note element? Or does it make more sense to go into its own element?
<date>12/11/2002</date>
Or, does it make sense for it to be separated into multiple elements?
<date>
<day>12</day>
<month>11</month>
<year>2002</year>
</date>
XML files provide data and metadata. DTD files and XML schema files provide metadata.
There are two ways to describe an XML document: XML Schemas and DTDs.
To summarize: An XML file is a file used to store data in the form of hierarchical elements. Data stored in XML files can be read by computer programs with the help of custom tags, which indicate the type of element.
Following the "Data goes in elements, metadata in attributes.", I would have made the Date a child element. You don't need to break it down into day, month, and year, because I think there's actually a way to specify in an XSD that an element must be a Date type. I think an example of "metadata" here would be a noteID
field or maybe a noteType
. Example:
<note id="NID0001234" type="reminder">
<date>2002-11-12</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
UPDATE: As many others have pointed out, it can be rather subjective. I try to separate the two by how they will be used. Data will usually be presented to the user, metadata will control the presentation and may be used internally for other purposes. But there are always exceptions...
The distinction between data and metadata is almost entirely subjective. One man's data is another's metadata. The "metadata in attributes" rule grew out of the markup world, where a rule of thumb was, if you remove all of the markup, and just leave the text, it should be a reasonable document. This meant attributes should be discardable, and elements essential. If you display XML in an uncomprehending browser, it will be treated this way.
But your XML (and most XML these days) likely won't be displayed to the user in an uncomprehending browser, so you can use better rules for how to design your XML.
For example, you can have multiple elements with the same name, but not multiple attributes. And whitespace is ignored in attributes, but not in elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With