xml 修改 xsd:dateTime 简单类型以使用不同的日期和时间分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1899581/
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
Modify xsd:dateTime simple type to use different date-and-time separator
提问by nd.
I have a legacy system that produces XML with timestamps similar to ISO 8601, but with space used to separate date and time. E.g. 2009-12-31 00:00:00. I would like to define a schema that defines the possible range for these timestamps. xsd:dateTimewould be well suited for that purpose, however, it uses Tas the separator between date and time. I cannot modify the legacy system to return timestamps using T as a separator.
我有一个遗留系统,它生成带有类似于 ISO 8601 的时间戳的 XML,但有用于分隔日期和时间的空间。例如2009-12-31 00:00:00。我想定义一个模式来定义这些时间戳的可能范围。xsd:dateTime将非常适合该目的,但是,它使用T作为日期和时间之间的分隔符。我无法修改旧系统以使用 T 作为分隔符返回时间戳。
Is there a way to have a simpleTypedefinition that derives from xsd:dateTime, but alters the separator or do I have to rely on a string with an appropriate pattern and human readable comments?
有没有一种方法可以simpleType定义从xsd:dateTime, 但改变分隔符的定义,或者我是否必须依赖具有适当模式和人类可读注释的字符串?
Update:As I understand, I can use a pattern for dateTime to restrict the range of dateTime objects for input, but this does not alter the separator character.
更新:据我所知,我可以使用 dateTime 的模式来限制用于输入的 dateTime 对象的范围,但这不会改变分隔符。
Example:
例子:
<xs:restriction base="xs:dateTime">
<xs:pattern value="[2].*:[0-9]{2}"/>
</xs:restriction>
This would only allow for dateTime with years starting with 2000 and without fractional seconds and time zone information.
这将只允许 dateTime 从 2000 年开始,并且没有小数秒和时区信息。
Summary of answers:
答案摘要:
It is not possible to use xs:dateTimeas the base type for this. It is however possible to use xs:stringand define a pattern.
不能xs:dateTime用作此的基本类型。然而,可以使用xs:string和定义模式。
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]"/>
</xs:restriction>
By using a string, of course, any automatic tools that use the Schema to create language bindings will also retrieve a string, conversion to the appropriate language type for date/time has to be done by hand.
当然,通过使用字符串,任何使用 Schema 创建语言绑定的自动工具也将检索字符串,必须手动将日期/时间转换为适当的语言类型。
回答by devuxer
You can do a regular expression pattern restriction on the xs:stringdata type.
您可以对xs:string数据类型进行正则表达式模式限制。
For example:
例如:
<xs:element name="specialDateTime">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9][0-9]/[0-9][0-9]/[0-9][0-9] [0-9][0-9]:[0-9][9-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Edit
编辑
I found this example at http://www.cs.wisc.edu/condor/classad/refman/node9.html. It looks like you canput a pattern restriction on a dateTime:
我在http://www.cs.wisc.edu/condor/classad/refman/node9.html找到了这个例子。看起来您可以对 a 设置模式限制dateTime:
<xsd:simpleType>
<xsd:restriction base="xsd:dateTime">
<xsd:pattern value="\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d[+\-]\d\d:\d\d" />
</xsd:restriction>
</xsd:simpleType>
Hope that helps.
希望有帮助。
回答by Francis Upton IV
I don't think you can derive something else from xsd:dateTime, as the XML processors will not be able to understand this. Your best bet is to use a string with the right pattern.
我认为您不能从 xsd:dateTime 派生出其他东西,因为 XML 处理器将无法理解这一点。最好的办法是使用具有正确模式的字符串。

