开发者

More and more examples saving text data (strings) in an attributes instead of XML text node. Why? [duplicate]

This question already has answers here: Closed 10 years ago.

Possible Duplicate:

XML attribute vs XML element

Can someone explain me why more and more examples that I see on the Internet, keeping the text data in an attribute value instead of text node? Example:

<Root>
    <Data Value="blaaa" />
<Root>

Instead of this:

<Root>
    <Data>Blaaa</Data>
</Root>

What are the benefits actually 开发者_StackOverflow中文版to use attributes? Thanks.


It's not about which is beneficial. Attribute and Element each have their own semantic meaning and purpose.

If you consider the information in question to be part of the essential material that is being expressed or communicated in the XML, put it in an element. For human-readable documents this generally means the core content that is being communicated to the reader. For machine-oriented records formats this generally means the data that comes directly from the problem domain. If you consider the information to be peripheral or incidental to the main communication, or purely intended to help applications process the main communication, use attributes. This avoids cluttering up the core content with auxiliary material. For machine-oriented records formats, this generally means application-specific notations on the main data from the problem-domain.

Source: http://www.ibm.com/developerworks/xml/library/x-eleatt.html


There are no major benefits aside from granularity, aesthetics, and personal preference. Personally I prefer the nested elements over attributes, I feel like it gives me more control on the application side. For more detailed reading here is an article from IBM: IBM and here is a quicker read: W3C. Truly it boils down to personal preference though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜