Solved

Blank Dates in xsd

Posted on 2002-05-02
5
476 Views
Last Modified: 2012-06-21
Is there a way to allow blank dates in xsd schema validation?

I have an attribute named DateOfBirth type = xsd:date and I may or may not have any data to put into it. The schema currently says this attribute is required but won't accept and date of "" or " ". Is the only solution to alter the scehma to make is not required and just not populate the attribute if I don't have a date of birth on my database?

Dave
0
Comment
Question by:howesd
5 Comments
 

Expert Comment

by:hohndove
ID: 6986238
Hi, Howesd!

What, if any, errors are you seeing?  I used the following Schema:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://www.people.org"
      xmlns="http://www.people.org"
      elementFormDefault="qualified">

  <xsd:element name="people">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="person" maxOccurs="unbounded"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="person" type="personType"/>

  <xsd:complexType name="personType">
    <xsd:attribute name="name" type="xsd:string" use="required"/>
    <xsd:attribute name="dob" type="xsd:date" use="required"/>
  </xsd:complexType>
</xsd:schema>

Using this Schema, I was able to create the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<people     xmlns="http://www.people.org"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.people.org test.xsd">

  <person name="Sam Smith" dob="1954/02/01"/>
  <person name="John Doe" dob=""/>
</people>

This XML validated for me with no errors, but I do get an error if I omit the dob attribute.  Does your Schema and XML look like this?

     -Jesse
0
 
LVL 1

Author Comment

by:howesd
ID: 6986288
Jesse

Our schema is quite similar to what you had - in the end we got round the problem by defining a new type as a union between a date field and an enumerated value that allows a space. It works OK. I can't take the credit for finding out how to do this - it was my mate Dave who sussed it

Dave



- <xsd:simpleType name="bslDate">
- <xsd:union>
- <xsd:simpleType>
  <xsd:restriction base="xsd:date" />
  </xsd:simpleType>
- <xsd:simpleType>
- <xsd:restriction base="xsd:token">
  <xsd:enumeration value="" />
  </xsd:restriction>
  </xsd:simpleType>
  </xsd:union>
  </xsd:simpleType>
0
 
LVL 6

Expert Comment

by:wbradney
ID: 6997005
hohndove,

Looks like your parser is broken (or at least non-conforming). xsd:date should ONLY allow strings of the format CCYY-MM-DD. Empty strings are not allowed.

The relevant section of the spec is at:
http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#date

howesd's solution is the typcial way to get around these kinds of restrictions, although it's much simpler (and semantically cleaner) to make the entire attribute optional, and infer from the fact that it's not present in the instance document that no date has been specified. If the attribute is present, then it MUST conform to the CCYY-MM-DD format.

Regards,
WMB
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 10298593
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ / Refund points

Please leave any comments here within the next four days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

rdcpro
EE Cleanup Volunteer
0
 

Accepted Solution

by:
amp072397 earned 0 total points
ID: 10338339
PAQed, with points refunded (100)

amp
Community Support Cleanup Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Xsl-fo show PDF when no results are returned 2 23
Add to XML (Powershell) 1 42
XSL FO - remove empty space and automatically size columns 6 19
Need a modeling tool 2 43
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question