Re: xsd:date and xsd:pattern together for custom data type

Hi Om,

> I've declared in the below format, but the XMLSpy tool is giving an error
> the attribute tag is invalid child.
>
>         <xsd:element name="eosdate">
>                 <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:attribute name="value" type="xsd:date"/>
>         </xsd:element>

Here, you have xsd:attribute as a direct child of xsd:element. You
can't do that. What you're trying to do here is say that the eosdate
element has simple *content* and an attribute. An element that has any
attributes is a *complex* type, so you need:

<xsd:element name="eosdate">
  <xsd:complexType>
    <xsd:simpleContent>
      ...
    </xsd:simpleContent>
  </xsd:complexType>
</xsd:element>

In order to say that the element's content is based on a restricted
version of a string with an attribute added, you need to have two
steps. First, create the simple type with the date format that you
want:

<xsd:simpleType name="date">
  <xsd:restriction base="xsd:token">
    <xsd:pattern value="[0-1][0-9]/[0-3][0-9]/[0-9]{2}" />
  </xsd:restriction>
</xsd:simpleType>

then *extend* that simple type by adding an attribute:

<xsd:element name="eosdate">
  <xsd:complexType>
    <xsd:simpleContent>
      <xsd:extension base="date">
        <xsd:attribute name="value" type="xsd:date" />
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>
</xsd:element>

This means that you can have things like:

  <eosdate value="2002-10-29">10/29/2002</eosdate>

Note that your current pattern doesn't restrict the content of eosdate
to valid dates, and there's nothing to guarantee that the date held in
the content of eosdate matches the date held in the value attribute of
eosdate.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Tuesday, 29 October 2002 10:27:31 UTC