xml 使用 XS:date 我想要格式为 YYYYMMDD 的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5039657/
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
Using XS:date i want date in format YYYYMMDD
提问by Praneel PIDIKITI
Using XSD i want to only accept date of the format YYYYMMDD in my xml field .. So how can i do that
使用 XSD 我只想在我的 xml 字段中接受格式 YYYYMMDD 的日期..那么我该怎么做
I saw this in an example will this work ??
我在一个例子中看到了这个,这行得通吗??
回答by Tomasz Nurkiewicz
XML schema definesdateTimeas ISO8601 with some exceptions and you should stick with this, otherwise you will get serious interoperability issues. If you want to send/receive date using different format, use simpleTypewith regular expression restriction and parse/format the date in your application code:
XML 模式定义dateTime为 ISO8601,但有一些例外,您应该坚持这一点,否则会遇到严重的互操作性问题。如果要使用不同的格式发送/接收日期,请使用simpleType正则表达式限制并在应用程序代码中解析/格式化日期:
<xs:simpleType name="CustomDate">
<xs:restriction base="xs:string">
<xs:pattern value="\d{8}"/>
</xs:restriction>
</xs:simpleType>
If you reallywant to mess around with built-in types (highly inadvisable), your XML framework/library might have some support for that. For instance in Java/JAXB you can apply custom transformers/formatters to any type, so that in client/server code you are still using Dateobject (not the 8-digit String), but it is marshalled/unmarshalled using your custom routine.
如果您真的想弄乱内置类型(非常不明智),您的 XML 框架/库可能会对此提供一些支持。例如,在 Java/JAXB 中,您可以将自定义转换器/格式化程序应用于任何类型,以便在客户端/服务器代码中您仍然使用Dateobject(不是 8-digit String),但使用您的自定义例程对其进行编组/解组。

