XML attribute vs XML element

At work we are being asked to create XML files to pass data to another offline application that will then create a second XML file to pass back in order to update some of our data. During the process we have been discussing with the team of the other application about the structure of the XML file.

The sample I came up with is essentially something like:

<INVENTORY>
   <ITEM serialNumber="something" location="something" barcode="something">
      <TYPE modelNumber="something" vendor="something"/> 
   </ITEM>
</INVENTORY>

The other team said that this was not industry standard and that attributes should only be used for meta data. They suggested:

<INVENTORY>
   <ITEM>
      <SERIALNUMBER>something</SERIALNUMBER>
      <LOCATION>something</LOCATION>
      <BARCODE>something</BARCODE>
      <TYPE>
         <MODELNUMBER>something</MODELNUMBER>
         <VENDOR>something</VENDOR>
      </TYPE>
   </ITEM>
</INVENTORY>

The reason I suggested the first is that the size of the file created is much smaller. There will be roughly 80000 items that will be in the file during transfer. Their suggestion in reality turns out to be three times larger than the one I suggested. I searched for the mysterious "Industry Standard" that was mentioned, but the closest I could find was that XML attributes should only be used for meta data, but said the debate was about what was actually meta data.

After the long winded explanation (sorry) how do you determine what is meta data, and when designing the structure of an XML document how should you decide when to use an attribute or an element?


I use this rule of thumb:

  • An Attribute is something that is self-contained, ie, a color, an ID, a name.
  • An Element is something that does or could have attributes of its own or contain other elements.
  • So yours is close. I would have done something like:

    EDIT : Updated the original example based on feedback below.

      <ITEM serialNumber="something">
          <BARCODE encoding="Code39">something</BARCODE>
          <LOCATION>XYX</LOCATION>
          <TYPE modelNumber="something">
             <VENDOR>YYZ</VENDOR>
          </TYPE>
       </ITEM>
    

    Some of the problems with 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 DTD
  • 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 used):

    <note day="12" month="11" year="2002" 
          to="Tove" from="Jani" heading="Reminder"  
          body="Don't forget me this weekend!"> 
    </note>
    

    Source: http://www.w3schools.com/xml/xml_dtd_el_vs_attr.asp


    "XML" stands for "eXtensible Markup Language". A markup language implies that the data is text, marked up with metadata about structure or formatting.

    XHTML is an example of XML used the way it was intended:

    <p><span lang="es">El Jefe</span> insists that you
        <em class="urgent">MUST</em> complete your project by Friday.</p>
    

    Here, the distinction between elements and attributes is clear. Text elements are displayed in the browser, and attributes are instructions about how to display them (although there are a few tags that don't work that way).

    Confusion arises when XML is used not as a markup language, but as a data serialization language, in which the distinction between "data" and "metadata" is more vague. So the choice between elements and attributes is more-or-less arbitrary except for things that can't be represented with attributes (see feenster's answer).

    链接地址: http://www.djcxy.com/p/66420.html

    上一篇: 是否有可能在XML中表示SQL RDB?

    下一篇: XML属性与XML元素