Reputation: 13
I have an XML element
<ManufactureDate>20150316</ManufactureDate>
that uses a custom date type element,
<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>
for validation, but now I want another element to have the same CustomDate
datatype but to give an input with time like below
<ExpirationDate>20150316T15:53:00</ExpirationDate>
Does anyone know how I can change the simpleType
to accept both types of formats?
Upvotes: 1
Views: 2181
Reputation: 630
As an addition to the solution proposed by @har07, I will propose the following:
<?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>
It uses the standard XSD date and dateTime formats, which I suspect would be the most standard thing to do rather than to invent a new format. I even think this should work:
<?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>
Upvotes: 0
Reputation: 13
Thanks for all the replies. I could solve this by changing the pattern.
I just used <xs:pattern value="(\d*)|(\d*T\d{2}:\d{2}:\d{2})"/>
to make it work.
it just didn't work for me before since I had missed the parenthesis earlier.
Thanks
Upvotes: 0
Reputation: 89325
One possible way is to create another simpleType
for custom date that comes with time based on your CustomDate
type definition :
<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>
then you can use xs:union
to accept both of the custom types, like so :
<xs:simpleType name="CustomDateOrDateTime">
<xs:union memberTypes="CustomDate CustomDateTime"/>
</xs:simpleType>
There are several other routes you can take, f.e change the regex pattern to accept both date with time and without time. Though, I don't know the exact requirement, i.e whether changing maxLength
restriction acceptable or not, etc.
Upvotes: 1
Reputation: 111726
I like @har07's idea to use xs:union
, but if you really want to modify your existing CustomDate
directly to accept an optional time component, you could use this:
<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>
Be aware that these regexp-based constraints only lexically approximate date and time datatypes. For example, where xs:date
would prohibit months greater than 12, these patterns would accept them.
Upvotes: 1