XSD 中禁止使用原始数据类型的空元素

发布于 2024-10-18 02:30:39 字数 510 浏览 2 评论 0原文

我在处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2024-10-25 02:30:39

在 XML Schema 规范中到底在哪里可以找到这个约束?

http://www.w3.org/TR/xmlschema-2/ #float-词法表示

浮点值有一个词法
表示由一个
尾数后面可选地跟随
字符“E”或“e”,后跟
指数。
...
指数和的表示形式
尾数必须遵循词汇规则
对于整数和小数。
...
特殊的价值观积极和
负无穷大和非数字
具有词汇表示形式 INF、-INF
和 NaN,分别。

因此,xs:float 至少需要一个 xs:decimal 尾数...

十进制有词法表示
由有限长度序列组成
小数位数 (#x30-#x39)
以句点分隔作为小数
指标。可选的前导标志是
允许。

...空字符串不是有效的 xs:decimal

如果您没有此元素的值,则应尽可能尝试不包含此元素。您的架构似乎允许省略此元素,因为 minOccurs 的值为 0。其他解决方案是插入合适的替换值,例如 0NaN

Where exactly can I find this constraint in the XML Schema specification?

http://www.w3.org/TR/xmlschema-2/#float-lexical-representation

float values have a lexical
representation consisting of a
mantissa followed, optionally, by the
character "E" or "e", followed by an
exponent.
...
The representations for exponent and
mantissa must follow the lexical rules
for integer and decimal.
...
The special values positive and
negative infinity and not-a-number
have lexical representations INF, -INF
and NaN, respectively.

So xs:float requires at least a mantissa that is a xs:decimal...

decimal has a lexical representation
consisting of a finite-length sequence
of decimal digits (#x30-#x39)
separated by a period as a decimal
indicator. An optional leading sign is
allowed.

...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 value 0. Other solution would be to insert a suitable replacement value, like 0 or NaN.

久伴你 2024-10-25 02:30:39

这不是一个明确的限制。
您应该能够将您的 xsd 更改为

<xsd:element name="myValue" type="xsd:float" minOccurs="0" default="0" />

然后能够为您的浮动提供一个空元素,而不会导致您的 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

<xsd:element name="myValue" type="xsd:float" minOccurs="0" default="0" />

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

if the element appears without any content, the schema processor provides the element with a value equal to that of the default attribute. However, if the element does not appear in the instance document, the schema processor does not provide the element at all.

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 ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文