Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 13th, 2004, 10:30 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Custom Date Formats in an XSD

I'm working my way through a family-tree type XML example (rather than copying the book example).

Why is the xsd:date type is so restrictive? The most common date formats used are mm-dd-yyyy and the UK version is dd-mm-yyyy.

I want to create a custom format like dd/mm/yyyy in my schema so that I can validate some dates. I have posted my attempt below.

<xsd:simpleType name="UKDateFormat">
<xsd:restriction base="xsd:string">
<xsd:pattern value="dd/mm/yyyy"/>

Has anyone successfully achieved this before?

Is it possible to assign two data types to an element? e.g. If my dates were to be in "dd/mm/yyyy" or "dd mmm yyyy" formats i'd still like it to pass the schema validation.

<xsd:element name="DATEOFBIRTH" type="xsd:UKDateFormat" minOccurs="0" />

Any tips would be most appreciated.

Many thanks,
Francis

 
Old September 13th, 2004, 10:53 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well to start with you could alter your pattern to:
Code:
<xsd:pattern value="d{2}[ /]d{2}[ /]d{4}"/>
Unfortunately this would also allow 19/11 1962 and 19 11/1962.
Perhaps a union type with both dd mm yyyy and dd/mm/yyyy styles.
Of course this doen't stop invalid dates such as 29 02 2003 or even more blatantly wrong ones.
There are a number of date regexes shown on the Web but it's actually a very difficult one to code. That's why you may be better sticking to the built in type and just formating between that and your preferred style for display purposes.



--

Joe (Co-author Beginning XML, 3rd edition)
 
Old September 13th, 2004, 10:58 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Joe,

I agree, it's a very complicated business.

My solution will need to be able to format both dates. I think I need to create a complex type with two restrictions, the first goes like this...

<xsd:element name="DATEOFBIRTH" minOccurs="0">
 <xsd:simpleType>
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="[0,1]{1}[0-9]{1}/[0-3]{1}[0-9]{1}/[0-9]{2}"/>
  </xsd:restriction>
 </xsd:simpleType>
</xsd:element>

the second is a bit more tricky...

 
Old September 13th, 2004, 11:47 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>Why is the xsd:date type is so restrictive?

I think the answer to this lies in the theory that XML should represent information in a way that's independent of presentation considerations. The XML Schema designers deliberately chose to force you to use a culturally-neutral format.

>The most common date formats used are mm-dd-yyyy

Really? I only know of one country in the world that uses that format.

>and the UK version is dd-mm-yyyy.

dd/mm/yyyy is more common in the UK, but most published documents are likely to write the month as a name.

>I want to create a custom format like dd/mm/yyyy in my schema so that >I can validate some dates. I have posted my attempt below.

It's easy enough to define a pattern such as \d{2}/\d{2}/\d{4}, but there's no way of rejecting 31/02/2004, or even 33/19/2004.

Has anyone successfully achieved this before?

>Is it possible to assign two data types to an element? e.g. If my >dates were to be in "dd/mm/yyyy" or "dd mmm yyyy" formats i'd still >like it to pass the schema validation.

Yes, you can do this with union types. Or (IIRC) you can define more than one pattern.




Michael Kay
http://saxon.sf.net/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Formats carumuga SQL Server 2000 4 January 2nd, 2007 07:20 PM
XML Date Formats tracyamgray XSLT 5 July 12th, 2006 10:17 PM
Trouble with date formats DrewMills Access VBA 3 March 7th, 2005 06:22 PM
SQL and Date Formats al_bllinky Access 3 August 28th, 2003 08:45 AM
Date formats in the forum Crispin Horsfield Forum and Wrox.com Feedback 1 August 3rd, 2003 08:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.