XSD 中禁止使用原始数据类型的空元素
我在处理 Web 服务响应时遇到 Apache CXF 解析错误。归根结底是返回一个空元素:
<myValue />
元素定义如下:
<xsd:element name="myValue" type="xsd:float" minOccurs="0">
现在我在 CXF 邮件列表上读到 XSD 规范不允许使用空值:
嗯,没有解决方法 这不是一个错误。一个空的 元素对于任何 Decimal 都无效 或日期类型或类似的东西。
因此,它应该抛出异常。
你期望它做什么?
现在问题来了:在 XML Schema 规范中到底在哪里可以找到这个约束?
I encountered a parsing error with Apache CXF while processing a webservice response. What it comes down to is an empty element being returned:
<myValue />
The element definition is as follows:
<xsd:element name="myValue" type="xsd:float" minOccurs="0">
Now I've read on the CXF mailing list that an empty value is not allowed by the XSD-spec:
Well, there isn't a workaround for
this as it's not a bug. An empty
element is not valid for any Decimal
or Date type or anything like that.
Thus, it SHOULD throw an exception.
What are you expecting it to do?
Now here comes the question: Where exactly can I find this constraint in the XML Schema specification?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://www.w3.org/TR/xmlschema-2/ #float-词法表示
因此,
xs:float
至少需要一个xs:decimal
尾数......空字符串不是有效的
xs:decimal
。如果您没有此元素的值,则应尽可能尝试不包含此元素。您的架构似乎允许省略此元素,因为
minOccurs
的值为0
。其他解决方案是插入合适的替换值,例如0
或NaN
。http://www.w3.org/TR/xmlschema-2/#float-lexical-representation
So
xs:float
requires at least a mantissa that is axs:decimal
......and an empty string is not a valid
xs:decimal
.If you don't have a value for this element, you should try not including this element, if possible. Your schema seems to allow omitting this element because
minOccurs
has value0
. Other solution would be to insert a suitable replacement value, like0
orNaN
.这不是一个明确的限制。
您应该能够将您的 xsd 更改为
然后能够为您的浮动提供一个空元素,而不会导致您的 xml 无效。
上面的示例意味着如果元素为空,则其值为 0。请注意,default 属性不适用于缺失的元素:缺失的元素只是缺失,无论它们是否具有声明的默认值。
http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints
到目前为止我还没有使用过这个,但是为了防止个人误读 w3c 规范,我已经与在线验证器进行了检查,确认带有默认值的空 xs:float 元素的 xml 被接受(至少被这个在线验证器接受) :http://www.freeformatter.com/xml-validator-xsd.html ).
This is not a definitive constraint.
You should be able to change your xsd to
And then be able to supply an empty element for your float without causing your xml to be invalid.
The above example means that if the element is empty, then its value is 0. Beware, default attribute does not apply on missing elements: missing elements are just missing, whether they have a declared default or not.
http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints
I have not used this till now, but to guard against a personal miss-reading of w3c specs, I have check with an online validator that an xml with an empty xs:float element having a default was accepted (at least by this online validator: http://www.freeformatter.com/xml-validator-xsd.html ).