参见英文答案 >
XML attribute vs XML element20个答案我正在学习
XML Attributes from W3Schools。
作者提到以下(强调我):
XML Elements vs. Attributes
06000
06001
In the first example sex is an attribute. In the last,sex is an element. Both examples provide the same information.
There are no rules about when to use attributes and when to use elements. Attributes are handy in HTML. In XML my advice is to avoid them. Use elements instead.
Avoid XML Attributes?
Some of the problems with using attributes are:
- attributes cannot contain multiple values (elements can)
- attributes cannot contain tree structures (elements can)
- attributes are not easily expandable (for future changes)
Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data.
那么作者的观点是一个着名的,还是这是XML的最佳实践?
应避免XML中的属性?
W3Schools还提到以下(强调我):
XML Attributes for Metadata
Sometimes ID references are assigned to elements. These IDs can be used to identify XML elements in much the same way as the ID attribute in HTML. This example demonstrates this:
06002
The ID above is just an identifier,to identify the different notes. It is not a part of the note itself.
What I’m trying to say here is that metadata (data about data) should be stored as attributes,and that data itself should be stored as elements.
属性或元素的使用通常由您尝试建模的数据决定。
例如,如果某个实体是数据的PART,那么建议使它成为一个元素。例如,员工的姓名是员工数据的重要部分。
现在,如果你想传递METADATA关于数据(提供关于数据的附加信息),但不是真正的数据的一部分,那么最好使它成为一个属性。
例如,让我们说每个员工都有一个后端处理所需的GUID,然后使它成为一个更好的属性(GUID不是一个传递真正有用的信息给某人看xml,但可能是其他目的所必需的)
没有规则说明某事应该是属性或元素。
它不需要AVOID属性在所有代价。有时他们比元素更容易建模。它真的取决于你试图代表的数据。