XML 最佳实践:属性与附加元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/241819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:11:16  来源:igfitidea点击:

XML best practices: attributes vs additional elements

xml

提问by CoolGravatar

What's the difference between the two and when should I use each:

两者之间有什么区别以及我应该何时使用它们:

<person>
     <firstname>Joe</firstname>
     <lastname>Plumber</lastname>
</person>

versus

相对

<person firstname="Joe" lastname="Plumber" />

Thanks

谢谢

采纳答案by Ray Lu

There are element centric and attribute centric XML, in your example, the first one is element centric, the second is attribute centric.

有以元素为中心和以属性为中心的 XML,在您的示例中,第一个是以元素为中心的,第二个是以属性为中心的。

Most of the time, these two patterns are equivalent, however there are some exceptions.

大多数情况下,这两种模式是等效的,但也有一些例外。

Attribute centric

以属性为中心

  • Smaller size than element centric.
  • Not very interoperable, since most XML parsers will think the user data is presented by the element, Attributes are used to describe the element.
  • There is no way to present nullable value for some data type. e.g. nullable int
  • Can not express complex type.
  • 比以元素为中心的尺寸更小。
  • 互操作性不强,因为大多数 XML 解析器会认为用户数据是由元素呈现的,因此使用 Attributes 来描述元素。
  • 无法为某些数据类型提供可为空的值。例如可为空的 int
  • 不能表达复杂类型。

Element centric

以元素为中心

  • Complex type can be only presented as an element node.
  • Very interoperable
  • Bigger size than attribute centric. (compression can be used to eliminated the size significantly)
  • Nullable data can be expressed with attribute xsi:nil="true"
  • Faster to parse since the parser only looks for elements for user data.
  • 复杂类型只能作为元素节点呈现。
  • 非常具有互操作性
  • 比以属性为中心的更大的尺寸。(压缩可用于显着消除尺寸)
  • 可空数据可以用属性 xsi:nil="true" 表示
  • 解析速度更快,因为解析器只查找用户数据的元素。

Practical

实际的

If you really care about the size of XML, use attribute whenever you can if it is appropriate, Leave the nullable, complex type and node which gonna hold large text value as elements, If you don't care the size of XML or you have compression enable during tranportation, stick with elements. It is more extensible.

如果您真的关心 XML 的大小,请在适当的情况下尽可能使用属性,保留可空的、复杂的类型和将保存大文本值的节点作为元素,如果您不关心 XML 的大小,或者您有在运输过程中启用压缩,坚持使用元素。它更具扩展性。

Background

背景

In DOT NET, XmlSerializer can serialize properties of objects into either attributes or elements. In the recently WCF framework, DataContract serializer can only serialize properties into elements and it is faster than XmlSerializer, the reason is obvious, it just needs to look for user data from elements while deserializing.

在 DOT NET 中,XmlSerializer 可以将对象的属性序列化为属性或元素。在最近的 WCF 框架中,DataContract 序列化器只能将属性序列化为元素,它比 XmlSerializer 更快,原因很明显,它只需要在反序列化时从元素中查找用户数据。

Here a article explains it as well Element vs attribute

这里有一篇文章也解释了 元素与属性

回答by Greg Hewgill

Sometime in the future when you add an <address>property, you won't want to make it an XML attribute. This is because an <address>might be a more complex element made up of street address, city, country, etc.

将来添加<address>属性时,您不会希望将其设为 XML 属性。这是因为 an<address>可能是一个更复杂的元素,由街道地址、城市、国家等组成。

For this reason, you may want to choose the first subelement form unless you're really sure that the attribute won't need to go much deeper. The first form allows for greater extensibility in the future.

出于这个原因,您可能希望选择第一个子元素形式,除非您真的确定该属性不需要更深入。第一种形式允许将来有更大的可扩展性。

If you're at all concerned about space, compress your XML.

如果您完全关心空间,请压缩您的 XML。

回答by JavaHopper

I found following information very helpful in explaining the choice of attributes vs elements in a short fashion

我发现以下信息非常有助于以简短的方式解释属性与元素的选择

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.

属性难以阅读和维护。将元素用于数据。对与数据无关的信息使用属性。

source : http://www.w3schools.com/xml/xml_attributes.asp

来源:http: //www.w3schools.com/xml/xml_attributes.asp

回答by LeopardSkinPillBoxHat

In my company, we would favour the 2nd approach.

在我的公司,我们倾向于第二种方法。

The way we think about it is that "firstname" and "lastname" are attributesof the "person" node, rather than sub-fieldsof the "person" node. It's a subtle difference.

我们考虑的方式是“firstname”和“lastname”是“person”节点的属性,而不是“person”节点的子字段。这是一个微妙的区别。

In my opinion the 2nd approach is more concise, and readability/maintainability is significantly improved, which is very important.

在我看来,第二种方式更简洁,可读性/可维护性得到显着提高,这一点非常重要。

Of course it would depend on your application. I don't think there is a blanket rule that covers all scenarios.

当然,这取决于您的应用程序。我不认为有涵盖所有情况的一揽子规则。

回答by Andru Luvisi

Attributes are not order sensitive. This may be an advantage or a disadvantage depending on your situation.

属性对顺序不敏感。根据您的情况,这可能是优势也可能是劣势。

Attributes cannot be duplicated. If "Joe" has two first names, then nodes are the only way to go.

属性不能重复。如果“Joe”有两个名字,那么节点是唯一的出路。