XML attribute vs XML element

前端 未结 20 2208
暖寄归人
暖寄归人 2020-11-22 01:34

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 ou

相关标签:
20条回答
  • 2020-11-22 01:58

    It's largely a matter of preference. I use Elements for grouping and attributes for data where possible as I see this as more compact than the alternative.

    For example I prefer.....

    <?xml version="1.0" encoding="utf-8"?>
    <data>
        <people>
             <person name="Rory" surname="Becker" age="30" />
            <person name="Travis" surname="Illig" age="32" />
            <person name="Scott" surname="Hanselman" age="34" />
        </people>
    </data>
    

    ...Instead of....

    <?xml version="1.0" encoding="utf-8"?>
    <data>
        <people>
            <person>
                <name>Rory</name>
                <surname>Becker</surname>
                <age>30</age>
            </person>
            <person>
                <name>Travis</name>
                <surname>Illig</surname>
                <age>32</age>
            </person>
            <person>
                <name>Scott</name>
                <surname>Hanselman</surname>
                <age>34</age>
            </person>
        </people>
    </data>
    

    However if I have data which does not represent easily inside of say 20-30 characters or contains many quotes or other characters that need escaping then I'd say it's time to break out the elements... possibly with CData blocks.

    <?xml version="1.0" encoding="utf-8"?>
    <data>
        <people>
            <person name="Rory" surname="Becker" age="30" >
                <comment>A programmer whose interested in all sorts of misc stuff. His Blog can be found at http://rorybecker.blogspot.com and he's on twitter as @RoryBecker</comment>
            </person>
            <person name="Travis" surname="Illig" age="32" >
                <comment>A cool guy for who has helped me out with all sorts of SVn information</comment>
            </person>
            <person name="Scott" surname="Hanselman" age="34" >
                <comment>Scott works for MS and has a great podcast available at http://www.hanselminutes.com </comment>
            </person>
        </people>
    </data>
    
    0 讨论(0)
  • 2020-11-22 01:58

    I agree with feenster. Stay away from attributes if you can. Elements are evolution friendly and more interoperable between web service toolkits. You'd never find these toolkits serializing your request/response messages using attributes. This also makes sense since our messages are data (not metadata) for a web service toolkit.

    0 讨论(0)
  • 2020-11-22 01:59

    How about taking advantage of our hard earned object orientation intuition? I usually find it is straight forward to think which is an object and which is an attribute of the object or which object it is referring to.

    Whichever intuitively make sense as objects shall fit in as elements. Its attributes (or properties) would be attributes for these elements in xml or child element with attribute.

    I think for simpler cases like in the example object orientation analogy works okay to figure out which is element and which is attribute of an element.

    0 讨论(0)
  • 2020-11-22 02:02

    I use this rule of thumb:

    1. An Attribute is something that is self-contained, i.e., a color, an ID, a name.
    2. 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>
    
    0 讨论(0)
  • 2020-11-22 02:02

    This is very clear in HTML where the differences of attributes and markup can be clearly seen:

    1. All data is between markup
    2. Attributes are used to characterize this data (e.g. formats)

    If you just have pure data as XML, there is a less clear difference. Data could stand between markup or as attributes.

    => Most data should stand between markup.

    If you want to use attributes here: You could divide data into two categories: Data and "meta data", where meta data is not part of the record, you want to present, but things like "format version", "created date", etc.

    <customer format="">
         <name></name>
         ...
    </customer>
    

    One could also say: "Use attributes to characterize the tag, use tags to provide data itself."

    0 讨论(0)
  • 2020-11-22 02:03

    There is no universal answer to this question (I was heavily involved in the creation of the W3C spec). XML can be used for many purposes - text-like documents, data and declarative code are three of the most common. I also use it a lot as a data model. There are aspects of these applications where attributes are more common and others where child elements are more natural. There are also features of various tools that make it easier or harder to use them.

    XHTML is one area where attributes have a natural use (e.g. in class='foo'). Attributes have no order and this may make it easier for some people to develop tools. OTOH attributes are harder to type without a schema. I also find namespaced attributes (foo:bar="zork") are often harder to manage in various toolsets. But have a look at some of the W3C languages to see the mixture that is common. SVG, XSLT, XSD, MathML are some examples of well-known languages and all have a rich supply of attributes and elements. Some languages even allow more-than-one-way to do it, e.g.

    <foo title="bar"/>;
    

    or

    <foo>
      <title>bar</title>;
    </foo>;
    

    Note that these are NOT equivalent syntactically and require explicit support in processing tools)

    My advice would be to have a look at common practice in the area closest to your application and also consider what toolsets you may wish to apply.

    Finally make sure that you differentiate namespaces from attributes. Some XML systems (e.g. Linq) represent namespaces as attributes in the API. IMO this is ugly and potentially confusing.

    0 讨论(0)
提交回复
热议问题