xml 架构更改日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13409567/
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
xml schema change date format
提问by aditya parikh
In the xml schema data type --> date allows date as yyyy-mm-dd format by default.
在 xml 模式数据类型中 --> 日期默认允许日期为 yyyy-mm-dd 格式。
How can we modify it so that it accepts yyyy/mm/dd format style instead ?
我们如何修改它以使其接受 yyyy/mm/dd 格式样式?
回答by Michael Kay
You can't get the standard xs:date type to accept formats other than the standard format. What you can do is to define a string data type with a pattern constraint; but it won't have date semantics, for example it will accept 1999/02/29.
您无法获得标准 xs:date 类型以接受标准格式以外的格式。您可以做的是定义具有模式约束的字符串数据类型;但它不会有日期语义,例如它会接受 1999/02/29。
Saxon's XSD processor has an experimental extension, saxon:preprocess, which allows you to change the lexical representation of a data type. Not many people use it, of course, because they want their schemas to be interoperable; but it does exactly what you want so I thought I would mention it.
Saxon 的 XSD 处理器有一个实验性扩展 saxon:preprocess,它允许您更改数据类型的词法表示。当然,使用它的人并不多,因为他们希望他们的模式是可互操作的;但它完全符合你的要求,所以我想我会提到它。
Another solution which is often overlooked, and which is perhaps more practical, is for your processing pipeline to do a transformation step before it does the validation step. This approach can be used to overcome many of the limitations of XML Schema, and it isn't widely enough known.
另一个经常被忽视但可能更实用的解决方案是让您的处理管道在执行验证步骤之前执行转换步骤。这种方法可以用来克服 XML Schema 的许多限制,但它的知名度还不够高。
回答by Sunil
You cant change the date type to a specific format but you can take it as a string and choose a specific format that you want.
您不能将日期类型更改为特定格式,但您可以将其作为字符串并选择所需的特定格式。
For the format of date MM/DD/YYYY. the code snippet is given below.
日期格式为MM/DD/YYYY。下面给出了代码片段。
<xs:element name="StartDate">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\d{2}[/]\d{2}[/]\d{4}"></xs:pattern>
<xs:length value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
回答by slassh
you can check this regex posted by melwil. it worked for me !
您可以查看 melwil 发布的这个正则表达式。它对我有用!
((0[1-9]|[1-2]\d|3[01])-(0[13578]|1[02])|([0-2]\d|30)-(0[469]|11)|(0[1-9]|1\d|2[0-8])-02)-\d{4}|(0[1-9]|[12]\d)-02-(([02468][048]|[13579][26])00|(\d{2}([02468][48]|[2468][048]|[13579][26])))

