title: Define an XSD element which can be a dateTime or empty with an attribute
body: <p>My question is almost exactly the same as <a href="https://stackoverflow.com/questions/8859702/define-an-element-with-attribute-and-optional-value?rq=1">this one</a>, but for a xs:dateTime type rather than a user defined element.</p>
  
  <p>An element in my XML (which I do not create) can look like:</p>
  
  <pre><code>&lt;parent&gt;
      ...
      &lt;start&gt;2012-01-01T00:00:00.000&lt;/start&gt;
      &lt;end&gt;2013-01-01T00:00:00.000&lt;/end&gt;
      ...
  &lt;/parent&gt;
  </code></pre>
  
  <p>-or-</p>
  
  <pre><code>&lt;parent&gt;
      ...
      &lt;start reference="a string" /&gt;
      &lt;end reference="a string" /&gt;
      ...
  &lt;/parent&gt;
  </code></pre>
  
  <p>In other words, within the parent element, the "start" and "end" fields can contain either a xs:dateTime value, or will be empty but have a "reference" attribute (either field might be one or the other within the parent, they're not necessarily both a reference or both a dateTime).  I've tried all sorts of ways to represent this in an XSD, but have not found a solution.  The closest I've come is (excerpted from a much larger XSD):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;xs:complexType name="DateOrRef"&gt;
      &lt;xs:simpleContent&gt;
  &lt;!--      &lt;xs:extension base="xs:dateTime"&gt; This does not validate--&gt;
        &lt;xs:extension base="xs:string"&gt;
          &lt;xs:attribute type="xs:string" name="reference" use="optional"/&gt;
        &lt;/xs:extension&gt;
      &lt;/xs:simpleContent&gt;
    &lt;/xs:complexType&gt;
    &lt;xs:complexType name="parent"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element minOccurs="0" name="start" type="DateOrRef" /&gt;
          &lt;xs:element minOccurs="0" name="end" type="DateOrRef" /&gt;
      &lt;/xs:sequence&gt;
    &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>which does validate, but does not restrict the node contents to be an xs:dateTime value.  If I change the extension base type to a xs:dateTime instead of xs:string as in the commented out line, the empty elements will no longer validate because the dateTime type is not allowed to be empty.</p>
  
  <p>How could I structure the XSD to validate these fields as xs:dateTime instead of xs:string?</p>