xml XSD 日期格式覆盖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1047958/
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 date format overriding
提问by Bhushan Bhangale
I am defining an XSD. I need to define an element which takes date in format yyyymmdd. How can I define a restriction in XSD to only accept this format?
我正在定义一个 XSD。我需要定义一个以 yyyymmdd 格式获取日期的元素。如何在 XSD 中定义只接受这种格式的限制?
回答by marc_s
You could always define it as a restricted simple type based on a string, restricted by a regular expression:
您始终可以将其定义为基于字符串的受限简单类型,受正则表达式限制:
<xs:simpleType name="FormattedDateType">
<xs:restriction base="xs:string">
<xs:pattern value="\d{8}"/>
</xs:restriction>
</xs:simpleType>
If you want to get really smart, you can tweak the regular expression to be even more of a match for a date (e.g. contains the info that month can only be 01 - 12 and so forth):
如果你想变得非常聪明,你可以调整正则表达式以使其更匹配日期(例如包含该月只能是 01 - 12 等的信息):
<xs:simpleType name="FormattedDateType">
<xs:restriction base="xs:string">
<xs:pattern value="\d{4}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])"/>
</xs:restriction>
</xs:simpleType>
Marc
马克
回答by Sunil
If you want the format of MM/DD/YYYY in xml then this code can help you for this format
如果您想要 xml 中的 MM/DD/YYYY 格式,那么此代码可以帮助您处理此格式
<xs:element name="StartDate">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\d{2}[/]\d{2}[/]\d{4}"/>
<xs:length value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

