Xml Schema规范是否使用默认值定义了可选属性的序列化? [英] Does the Xml Schema spec define the serialization of optional attributes with a default?

查看:79
本文介绍了Xml Schema规范是否使用默认值定义了可选属性的序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,Xml Schema规范指定optional attribute with a default value解析:如果xml文档中没有这样的属性,则在使用XSD进行解析时,它们似乎存在并且具有该默认值.

Firstly, the Xml Schema spec does specify the parsing of an optional attribute with a default value: if such an attribute is absent in an xml document, then when parsed with the XSD, they appear to be present, and having that default value.

第二,一些工具将此思想应用于序列化,以便省略具有默认值的属性(换句话说:给定an optional attribute with a default value,当一个属性具有该值时,将其省略).

Secondly, some tools apply this idea to serialization, so that attributes with default values are omitted (in other words: given an optional attribute with a default value, when an attribute has that value, it is omitted).

但是,据我所知,在Xml架构中未指定此序列化行为.但这是一个复杂的规范,也许我错过了-规范定义了optional attribute with a default value的序列化吗?

However, as far as I can tell, this serialization behaviour is not specified in the Xml Schema. But it's a complex spec, and maybe I missed it - does the spec define serialization of optional attribute with a default value?

如果没有,也许它已经成为了事实上的标准-许多工具都可以实现这种行为吗?我已经看到Microsoft和MarkLogic做到了:

If not, maybe it has become a de facto standard - do many tools implement this behaviour? I've seen that Microsoft and MarkLogic do it:

  • C# XML Serialization of the default values of optional attributes
  • ".net" (in general?) Including xml attribute when the value is the same as the default
  • marklogic Default Schema Values in MarkLogic

但不是JAXB:

  • not JAXB JAXB Avoid saving default values

最后-也许在这里我在主观观念的悬崖上徘徊-行为应该是什么?

Finally, - and perhaps here I teeter on the precipice of subjectivity - what should be the behaviour?

省略具有默认值的属性可以减少混乱,尤其是在默认值是期望值和通用值的情况下.例如XSD本身中的minOccursmaxOccurs默认为1-如果您一直在处理XSD,然后序列化结果,使它们突然出现在每个<element><sequence><choice>等上使其可读性降低.

Omitting attributes with default values reduces clutter, especially if the default value is expected and common. e.g. minOccurs and maxOccurs default to 1 in an XSD itself - if you've been processing an XSD, and then serialize the result, having them suddenly appear on every <element>, <sequence>, <choice> etc makes it less readable.

OTOH,在对象和数据结构中,如果有默认值,那么我们希望它在此存在.因此,当具有默认值的属性被忽略时,可能会令人惊讶-如上述SO问题所示.

OTOH, in objects and data structures, if there is a default value, we expect it to be there if we look at it. Therefore, it can be surprising when an attribute that has a default value is omitted - as the above SO questions show.

我认为这可能取决于您将结果视为数据还是数据表示形式.使用表示形式,可以很方便地对其进行修改以使其更具可读性.但是对于数据,您希望它完全数据.有想法吗?

I think it might depend on whether you consider the result to be data, or a representation of data. With a representation, it can be very convenient to modify it to make it more readable. But with data, you expect it to exactly be the data. Thoughts?

推荐答案

对于第一个问题,答案是否定的-这不是XSD规范所关心的.

For the first question, the answer is NO - it is not something the XSD spec is concerned about.

2,仅由于有太多工具箱在做这种事情……什么构成许多",哪些被认为是相关的,等等,这很难从一个人那里得到答案.

2nd, it is kind of hard to get an answer from a single person, simply because there are so many toolkits doing this sort of thing… what constitutes "many", which are to be considered relevant, etc.

3rd…在我的工作中,我(过去)经常使用XSD中的默认值来建议 NOT .论据是,默认值通过强加使用支持XSD的处理器或强制使用方应用程序中的其他逻辑/设置来限制XML实例的正确使用.而且,别忘了那些将默认值视为邪恶的人-难以发现的错误的来源,例如测试/生产环境中的配置项.

3rd… In my work I (used to) bump quite often into the recommendation of NOT using defaults in XSDs. The argument was that defaults restrict the correct use of an XML instance by imposing the use of an XSD-aware processor OR forcing additional logic/settings in the consumer application. And, let's not forget those people that consider defaults as evil - the source of hard-to-find bugs, e.g. configuration items in test/production environments.

话虽如此,对我而言,序列化默认值的工具很好地表明了采用防御性编程"风格的迹象;无论模式语言如何,它都能促进XML的互操作性.

That being said, to me a tool serializing defaults is a good indication of employing a "defensive programming" style; it promotes the interoperability of XML irrespective of a schema language.

因此,我相信任何认真的XSD感知XML序列化程序都应该提供启用/禁用默认值序列化的选项.我将默认设置为ON(序列化).

So, I believe that any serious XSD-aware XML serializer SHOULD provide an option to enable/disable the serialization of default values. I would set the default to ON (serialize).

在您拥有专门的处理器的情况下,这些处理器在设计上必须理解并遵守XSD定义的语言,因此,实际上线路上的默认值可能会被认为是混乱的(您的最小/最大出现参考);可以选择关闭这些值应该对此有所帮助.

Where you have specialized processors that by design are bound to understand and comply with a language as defined by XSD, then indeed default values on the wire may be perceived as clutter (your min/max occurrence reference); having the option to turn off these values should help there.

这篇关于Xml Schema规范是否使用默认值定义了可选属性的序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆