Xml 接受日期或日期时间的自定义XSD简单类型

Xml 接受日期或日期时间的自定义XSD简单类型,xml,xsd,xsd-validation,Xml,Xsd,Xsd Validation,我有一个XML元素 20150316 使用自定义日期类型元素的 <xs:simpleType name="CustomDate"> <xs:restriction base="xs:string"> <xs:maxLength value="8"/> <xs:whiteSpace value="collapse"/> <xs:pattern value="\d*"/> </

我有一个XML元素

20150316

使用自定义日期类型元素的

<xs:simpleType name="CustomDate">
    <xs:restriction base="xs:string">
       <xs:maxLength value="8"/>
       <xs:whiteSpace value="collapse"/>
       <xs:pattern value="\d*"/>
    </xs:restriction>
/xs:simpleType>

有人知道如何更改
simpleType
以接受这两种格式吗?

一种可能的方法是根据您的
CustomDate
类型定义为随时间而来的自定义日期创建另一个
simpleType

<xs:simpleType name="CustomDateTime">
    <xs:restriction base="xs:string">
       <xs:maxLength value="17"/>
       <xs:whiteSpace value="collapse"/>
       <xs:pattern value="\d*T\d\d:\d\d:\d\d"/>
    </xs:restriction>
</xs:simpleType>

您还可以选择其他几种方法,例如更改正则表达式模式,以接受有时间日期和无时间日期。虽然,我不知道确切的要求,即更改
maxLength
限制是否可以接受,等等。

我喜欢@har07使用
xs:union
的想法,但是如果您确实想直接修改现有的
CustomDate
以接受可选的时间组件,您可以使用以下方法:

<xs:simpleType name="CustomDate">
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="collapse"/>
    <xs:pattern value="\d{8}(T\d\d(:\d\d){2})?"/>
  </xs:restriction>
</xs:simpleType>


请注意,这些基于regexp的约束仅在词汇上近似于日期和时间数据类型。例如,如果
xs:date
禁止超过12个月,这些模式将接受它们。

感谢所有回复。我可以通过改变模式来解决这个问题

我只是用
让它工作

因为我之前错过了括号,所以它以前对我不起作用


感谢

作为@har07提出的解决方案的补充,我将提出以下建议:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xml:lang="DA">
  <xs:element name="myDateTime" type="CustomDateTime" />
  <xs:simpleType name="DateType">
    <xs:restriction base="xs:date" >
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="DateTimeType">
    <xs:restriction base="xs:dateTime" >
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="CustomDateTime">
    <xs:union memberTypes="DateType DateTimeType"/>
  </xs:simpleType>
</xs:schema>

它使用标准的XSD日期和日期时间格式,我认为这是最标准的做法,而不是发明一种新的格式。我甚至认为这应该奏效:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xml:lang="DA">
  <xs:element name="SlutDato" type="CustomDateTime" />
  <xs:simpleType name="CustomDateTime">
    <xs:union memberTypes="xs:dateTime xs:date"/>
  </xs:simpleType>
</xs:schema>

使用
xs:union
可以轻松地继续分别使用这两种类型,因此更喜欢此解决方案,但@har07也提到了单一regexp方法。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xml:lang="DA">
  <xs:element name="myDateTime" type="CustomDateTime" />
  <xs:simpleType name="DateType">
    <xs:restriction base="xs:date" >
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="DateTimeType">
    <xs:restriction base="xs:dateTime" >
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="CustomDateTime">
    <xs:union memberTypes="DateType DateTimeType"/>
  </xs:simpleType>
</xs:schema>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xml:lang="DA">
  <xs:element name="SlutDato" type="CustomDateTime" />
  <xs:simpleType name="CustomDateTime">
    <xs:union memberTypes="xs:dateTime xs:date"/>
  </xs:simpleType>
</xs:schema>