xml XSD 验证 xs:date 和 xs:dateTime 的错误格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11691158/
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
XSD validates wrong format of xs:date and xs:dateTime
提问by BentCoder
When I set the yearsection like below (20512 or anything like this), XSD still validates XML.
当我像下面这样设置年份部分(20512 或类似的东西)时,XSD 仍然会验证 XML。
Any idea.
任何的想法。
Is this a flaw or do I have to use simpleTypewith given pattern?
这是一个缺陷还是我必须使用具有给定模式的simpleType?
Thanks
谢谢
XSD
XSD
<xs:attribute name="date" type="xs:date" />
<xs:attribute name="timestamp" type="xs:dateTime" />
XML
XML
<store date="20512-07-11" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="items.xsd">
<store timestamp="20512-07-11T21:50:16" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="items.xsd">
采纳答案by Petru Gardea
It is NOTflaw in your validator (btw, you should indicate which validator you're using). Other validators (mis)behave the way you seem to expect; for e.g., .NET would complain The value '20512-07-11' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:date' - The string '20512-07-11' is not a valid XsdDateTime value.
这不是您的验证器中的缺陷(顺便说一句,您应该指出您正在使用哪个验证器)。其他验证器(错误)的行为与您似乎期望的一样;例如,.NET 会抱怨The value '20512-07-11' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:date' - The string '20512-07-11' is not a valid XsdDateTime value.
See this sectionof the XSD spec, section D3.3 which clearly states that your value is valid.
请参阅XSD 规范的这一部分,即 D3.3 部分,其中明确指出您的值是有效的。
回答by Michael Kay
Sorry, but why do you consider 20512-07-11 to be an invalid date? Astronomers might be very keen to maintain data showing that an eclipse will occur on that date.
抱歉,您为什么认为 20512-07-11 是无效日期?天文学家可能非常热衷于维护显示日食将在该日期发生的数据。
If you want to limit your dates to a particular range, e.g before the year 2100, then you should use a simpleType derived from xs:date that restricts the range of values using a maxExclusive facet.
如果您想将日期限制在特定范围内,例如在 2100 年之前,那么您应该使用从 xs:date 派生的 simpleType,它使用 maxExclusive facet 限制值的范围。

