W3C home > Mailing lists > Public > xmlschema-dev@w3.org > February 2011

Creating a User-Defined Data Type that is a Collection of User-Defined simpleTypes

From: Costello, Roger L. <costello@mitre.org>
Date: Fri, 25 Feb 2011 05:41:09 -0500
To: "xmlschema-dev@w3.org" <xmlschema-dev@w3.org>
Message-ID: <9E51F88D5247B648908850C35A3BBB50052E530B8C@IMCMBX3.MITRE.ORG>
Hi Folks,

Decoupling the definition of a simpleType from an element or attribute declaration is very useful. For example, here I define a family name simpleType:

    <xs:simpleType name="Family-name">
        <xs:restriction base="xs:string">
            <xs:minLength value="1" />
            <xs:maxLength value="100" />
            <xs:pattern value="[a-zA-Z' \.-]+" />

Now that it is defined, I can declare any number of elements or attributes to be of that type; for example: 

    <xs:element name="Family-name" type="Family-name" />

    <xs:element name="Surname" type="Family-name" />

    <xs:element name="Last-name" type="Family-name" />

That's nice! 

The definition of the user-defined simpleType and the declaration of the elements are completely decoupled. The Family-name simpleType is a reusable data type.

Suppose I define 2 more simpleTypes. A simpleType for Middle initial: 

    <xs:simpleType name="Middle-initial">
        <xs:restriction base="xs:string">
            <xs:length value="1" />
            <xs:pattern value="[A-Z]+" />

And a simpleType for Given name:

    <xs:simpleType name="Given-name">
        <xs:restriction base="xs:string">
            <xs:minLength value="1" />
            <xs:maxLength value="100" />
            <xs:pattern value="[a-zA-Z' \.-]+" />

Thus there are 3 simpleTypes. Collectively, they make up a user-defined "Name" data type.  

Of course, this Name data type is not a simpleType. It is made up of three simpleTypes. 

XML Schema does not have a way to express this so I propose a new capability, <xs:crossProduct>. Here is how to define the Name data type:

    <xs:crossProduct name="Name">
        <xs:componentType ref="Given-name" />
        <xs:componentType ref="Middle-initial" />
        <xs:componentType ref="Family-name" />

That defines a data type that is a cross product of 3 simpleTypes.

Now that it is defined, I can declare any number of elements to be of that data type; for example:

    <xs:element name="Name" type="Name">
        <xs:element name="First" type="Name.Given-name" />
        <xs:element name="MI" type="Name.Middle-initial" />
        <xs:element name="Last" type="Name.Family-name" />

Here's a different assignment of element names to the data type and its components: 

    <xs:element name="Customer" type="Name">
        <xs:element name="Given" type="Name.Given-name" />
        <xs:element name="MI" type="Name.Middle-initial" />
        <xs:element name="Surname" type="Name.Family-name" />

Here's how the latter would appear in an XML instance document:


The important thing to notice is that the data type definition is completely decoupled from the assignment of element names to the component fields of the data type.

To recap: just like xs:simpleType enables the creation of reusable (simple) data types, xs:crossProduct enables the creation of reusable (cross product) data types.


Received on Friday, 25 February 2011 10:41:45 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 23:15:58 UTC