Web服务API设计:XML元素与属性[重复]

时间:2022-09-16 11:13:49

This question already has an answer here:

这个问题在这里已有答案:

I'm designing an API for a web service and I can't decide between using XML attributes, elements or a mixed architecture.

我正在为Web服务设计API,我无法决定使用XML属性,元素还是混合体系结构。

Let me show you an example. Let's assume I have an object called Domain. This model has 3 properties (tld, sld, trd and the name itself) and a method valid? that returns true if the domain is valid.

让我举个例子。我们假设我有一个名为Domain的对象。这个模型有3个属性(tld,sld,trd和名称本身)和一个有效的方法?如果域有效,则返回true。

# I'm using ruby but 
# consider this as pseudo-code
class Domain
  attr_accessor :tld, :sld, :trd, :name

  def valid?
    true # force true
  end
end

My api called /domain/parser takes a domain in input and returns the parsed response. The first possibility is to use an element for every domain attribute.

我的api调用/ domain / parser在输入中获取一个域并返回解析后的响应。第一种可能性是为每个域属性使用一个元素。

<result>
  <domain>
    <name>www.google.it</name>
    <tld>it</tld>
    ...
    <valid>true</true>
  </domain>
</result>

But some interfaces use attributes.

但是有些接口使用属性。

<result>
  <domain tld="it" sld="google.com" trd="www" rule="*.foo" name="www.google.it" valid="true" />
</result>

And don't forget attributes and value.

不要忘记属性和价值。

<result>
  <domain tld="it" sld="google.com" trd="www" rule="*.foo" name="www.google.it" valid="true">
    www.google.it
  </domain>
</result>

In your opinion, which is the more powerful, flexible and expressive choice? Also, consider the response will be served in XML and JSON (soon).

在您看来,哪个更强大,更灵活,更富有表现力?另外,请考虑响应将以XML和JSON(很快)提供。

4 个解决方案

#1


8  

The pattern I use is:

我使用的模式是:

  • elements are for data
  • 元素用于数据
  • attribute are for meta data (i.e. data about the data)
  • 属性用于元数据(即有关数据的数据)

if you use XSD schema's most if not all of your meta data should go in there, but if you don't attributes are good place for it.

如果您使用XSD架构,那么大多数(如果不是所有的)元数据都应该放在那里,但如果您没有属性,那么它就是好地方。

So with your example I might do something like this:

所以用你的例子我可能会这样做:

<result>
  <domain valid="true">
    <name>www.google.it</name>
    <tld>it</tld>
    ...
  </domain>
</result>

#2


5  

WCF's designers chose to avoid attributes, mostly for performance reasons. The WCF default serializer, the DataContractSerializer, doesn't support attributes (so that might be something to take into consideration), but it's roughly 10% faster than the more flexible XmlSerializer in .NET.

WCF的设计者选择避免属性,主要是出于性能原因。 WCF默认序列化程序DataContractSerializer不支持属性(因此可能需要考虑),但它比.NET中更灵活的XmlSerializer快10%左右。

So if you ever see yourself serving up content to be consumed by a WCF client, you will likely try to steer clear of attributes, if ever possible.

因此,如果您看到自己提供要由WCF客户端使用的内容,您可能会尽可能避免使用属性。

Attributes are only ever going to be "atomic", e.g. a string, an int etc. - elements offer much more flexibility that way. Also, attributes never exist on their own - they're always tacked onto an element.

属性永远只是“原子的”,例如一个字符串,一个int等 - 元素提供了更多的灵活性。而且,属性从不存在 - 它们总是被添加到元素上。

From my personal experience and personal preference, I would most likely try to use mostly elements and avoid attributes as much as I can. But that's really just a personal preference and taste - attributes are absolutely valid in XML, no problem.

根据我的个人经验和个人喜好,我很可能会尽量使用元素并尽可能地避免使用属性。但这真的只是个人偏好和品味 - 属性在XML中绝对有效,没问题。

#3


3  

It's mainly a matter of taste, but there are some technical considerations. Attributes are slightly more restricted in what characters they can contain. They have the advantage (?) that order is immaterial but they cannot be repeated. This may slightly influence your choice according to what toolset you have

这主要是品味问题,但也有一些技术因素。属性稍微受限于它们可以包含的字符。它们的优点是(?)订单无关紧要,但不能重复。根据您拥有的工具集,这可能会略微影响您的选择

#4


3  

If your data can be thought of as having two levels, where one is the core data and the other is some sort of metadata (e.g. labels for certain elements) then you might want to use elements for the former and attributes for the latter, e.g.:

如果您的数据可以被认为有两个级别,其中一个是核心数据,另一个是某种元数据(例如某些元素的标签),那么您可能希望使用前者的元素和后者的属性,例如:

<result id="1">
  <domain type="default">
    <name unique="false">www.google.it</name>
    <tld>it</tld>
    ...
    <valid>true</true>
  </domain>
</result>

Typically if you strip all the attributes out the remaining data still looks meaningful.

通常,如果您删除所有属性,则剩余数据仍然看起来很有意义。

Another rule i sometimes use is attributes for summary data and elements for the rest. Then if i want to sent a summary list for instance i just send the top element plus its attributes and leave out the contained elements. E.g.

我有时使用的另一个规则是摘要数据的属性和其余的元素。然后,如果我想发送一个摘要列表,例如我只发送顶部元素及其属性,并省略所包含的元素。例如。

<result>
  <domain name="www.google.it">
    <tld>it</tld>
    ...
    <valid>true</true>
  </domain>
</result>

which in a list becomes:

在列表中变为:

<results>
  <domain name="www.google.it" />
  <domain name="www.google.co.uk" />
  <domain name="www.google.com" />
</results>

Alternatively if neither of those cases apply then you might just want to use elements for anything that has internal structure or where the order is important, and attributes for everything else, as per your second XML example.

或者,如果这两种情况都不适用,那么根据您的第二个XML示例,您可能只想将元素用于具有内部结构或顺序重要的任何内容,以及其他所有内容的属性。

#1


8  

The pattern I use is:

我使用的模式是:

  • elements are for data
  • 元素用于数据
  • attribute are for meta data (i.e. data about the data)
  • 属性用于元数据(即有关数据的数据)

if you use XSD schema's most if not all of your meta data should go in there, but if you don't attributes are good place for it.

如果您使用XSD架构,那么大多数(如果不是所有的)元数据都应该放在那里,但如果您没有属性,那么它就是好地方。

So with your example I might do something like this:

所以用你的例子我可能会这样做:

<result>
  <domain valid="true">
    <name>www.google.it</name>
    <tld>it</tld>
    ...
  </domain>
</result>

#2


5  

WCF's designers chose to avoid attributes, mostly for performance reasons. The WCF default serializer, the DataContractSerializer, doesn't support attributes (so that might be something to take into consideration), but it's roughly 10% faster than the more flexible XmlSerializer in .NET.

WCF的设计者选择避免属性,主要是出于性能原因。 WCF默认序列化程序DataContractSerializer不支持属性(因此可能需要考虑),但它比.NET中更灵活的XmlSerializer快10%左右。

So if you ever see yourself serving up content to be consumed by a WCF client, you will likely try to steer clear of attributes, if ever possible.

因此,如果您看到自己提供要由WCF客户端使用的内容,您可能会尽可能避免使用属性。

Attributes are only ever going to be "atomic", e.g. a string, an int etc. - elements offer much more flexibility that way. Also, attributes never exist on their own - they're always tacked onto an element.

属性永远只是“原子的”,例如一个字符串,一个int等 - 元素提供了更多的灵活性。而且,属性从不存在 - 它们总是被添加到元素上。

From my personal experience and personal preference, I would most likely try to use mostly elements and avoid attributes as much as I can. But that's really just a personal preference and taste - attributes are absolutely valid in XML, no problem.

根据我的个人经验和个人喜好,我很可能会尽量使用元素并尽可能地避免使用属性。但这真的只是个人偏好和品味 - 属性在XML中绝对有效,没问题。

#3


3  

It's mainly a matter of taste, but there are some technical considerations. Attributes are slightly more restricted in what characters they can contain. They have the advantage (?) that order is immaterial but they cannot be repeated. This may slightly influence your choice according to what toolset you have

这主要是品味问题,但也有一些技术因素。属性稍微受限于它们可以包含的字符。它们的优点是(?)订单无关紧要,但不能重复。根据您拥有的工具集,这可能会略微影响您的选择

#4


3  

If your data can be thought of as having two levels, where one is the core data and the other is some sort of metadata (e.g. labels for certain elements) then you might want to use elements for the former and attributes for the latter, e.g.:

如果您的数据可以被认为有两个级别,其中一个是核心数据,另一个是某种元数据(例如某些元素的标签),那么您可能希望使用前者的元素和后者的属性,例如:

<result id="1">
  <domain type="default">
    <name unique="false">www.google.it</name>
    <tld>it</tld>
    ...
    <valid>true</true>
  </domain>
</result>

Typically if you strip all the attributes out the remaining data still looks meaningful.

通常,如果您删除所有属性,则剩余数据仍然看起来很有意义。

Another rule i sometimes use is attributes for summary data and elements for the rest. Then if i want to sent a summary list for instance i just send the top element plus its attributes and leave out the contained elements. E.g.

我有时使用的另一个规则是摘要数据的属性和其余的元素。然后,如果我想发送一个摘要列表,例如我只发送顶部元素及其属性,并省略所包含的元素。例如。

<result>
  <domain name="www.google.it">
    <tld>it</tld>
    ...
    <valid>true</true>
  </domain>
</result>

which in a list becomes:

在列表中变为:

<results>
  <domain name="www.google.it" />
  <domain name="www.google.co.uk" />
  <domain name="www.google.com" />
</results>

Alternatively if neither of those cases apply then you might just want to use elements for anything that has internal structure or where the order is important, and attributes for everything else, as per your second XML example.

或者,如果这两种情况都不适用,那么根据您的第二个XML示例,您可能只想将元素用于具有内部结构或顺序重要的任何内容,以及其他所有内容的属性。