0

I have seen XML Schema element with attributes containing only text but I have an element that's an xs:dateTime instead.

The document I'm trying to write a schema for looks like this:

<web-campaigns>
  <web-campaign>
    <id>1231</id>
    <start-at nil="true"/>
  </web-campaign>
  <web-campaign>
    <id>1232</id>
    <start-at>2009-08-08T09:00:00Z</start-at>
  </web-campaign>
</web-campaigns>

Sometimes the xs:dateTime element has content, sometimes it doesn't.

What I have so far (which doesn't validate yet) is:

<xs:element name="start-at">
  <xs:complexType mixed="true">
    <xs:simpleContent>
      <xs:extension base="xs:dateTime">
        <xs:attribute name="nil" default="false" type="xs:boolean" use="optional" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

If I replace xs:dateTime with xs:string, I can validate the document just fine, but I really want an xs:dateTime, to indicate to consumers what's in that element. I tried with/without mixed="true" as well, to no avail.

If it makes a difference, I validate using xmllint (on Mac OS X 10.5) and XML Schema Validator

Community
  • 1
  • 1
François Beausoleil
  • 16,265
  • 11
  • 67
  • 90

2 Answers2

4

you can define your own types as union of types.

1/ define the "empty" type as a string that only allows "" ähm nothing :)

<xs:simpleType name="empty">
  <xs:restriction base="xs:string">
    <xs:enumeration value=""/>
  </xs:restriction>
</xs:simpleType>

2/ next define a type that allows date AND empty

<xs:simpleType name="empty-dateTime">
  <xs:union memberTypes="xs:dateTime empty"/>
</xs:simpleType>

3/ declare all your nullable datetime elements as type="empty-dateTime"

robbie
  • 41
  • 2
1

You need

<xs:element name="start-at" minOccurs="0">

mixed-mode isn't relevant to your situation, you don't need that. By default, minOccurs="1", i.e. the element is mandatory.

With minOccurs="0", you either specify the element with content, or not at all. If you want to be able to permit <start-at/>, then you cannot use xs:dateTime.

skaffman
  • 398,947
  • 96
  • 818
  • 769