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

Examples of all the different ways of creating XML Schema simpleTypes

From: Costello, Roger L. <costello@mitre.org>
Date: Fri, 24 Jun 2011 13:53:49 -0400
To: "xmlschema-dev@w3.org" <xmlschema-dev@w3.org>
Message-ID: <9E51F88D5247B648908850C35A3BBB500599FBB2B5@IMCMBX3.MITRE.ORG>
Hi Folks,

Below is an XML Schema. It contains examples of all the different ways of creating simpleTypes. Please let me know if there are other ways that I haven't accounted for.  /Roger


<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                     targetNamespace="http://www.example.org"
                     xmlns="http://www.example.org"
                     elementFormDefault="qualified">
        
    <!-- 
        Lists
        1. An unbounded list of integers
        2. A list of integers, constrained to 10 items in the list
        3. A restriction of a type that is a list of integers, constrained to 5 items of the type
        4. A list of strings that are constrained by a regex and a maxLength of 15, and the list is constrained to 4 items
    -->
    
    <!-- ******************************** -->
    <!-- 1. An unbounded list of integers -->
    <!-- ******************************** -->
    
    <xsd:element name="Numbers" type="Numbers" />
    
    <xsd:simpleType name="Numbers">
        <xsd:list itemType="xsd:integer" />
    </xsd:simpleType>
    
    <!-- ********************************************************** -->
    <!-- 2. A list of integers, constrained to 10 items in the list -->
    <!-- ********************************************************** -->
    
    <xsd:element name="Ten-Numbers" type="Ten-Numbers" />
    
    <xsd:simpleType name="Ten-Numbers">
        <xsd:restriction>
            <xsd:simpleType>
                <xsd:list itemType="xsd:integer" />
            </xsd:simpleType>
            <xsd:length value="10"/>
        </xsd:restriction>
    </xsd:simpleType>
    
    <!-- ***************************************************************************************** -->
    <!-- 3. A restriction of a type that is a list of integers, constrained to 5 items of the type -->
    <!-- ***************************************************************************************** -->
    
    <xsd:element name="Five-Numbers" type="Five-Numbers" />
    
    <xsd:simpleType name="Five-Numbers">
        <xsd:restriction base="Numbers">
            <xsd:length value="5"/>
        </xsd:restriction>
    </xsd:simpleType>
    
    <!-- ****************************************************************************************************************** -->
    <!-- 4. A list of strings that are constrained by a regex and a maxLength of 15, and the list is constrained to 4 items -->
    <!-- ****************************************************************************************************************** -->

    <xsd:element name="Names" type="Names" />
    
    <xsd:simpleType name="Names">
        <xsd:restriction>
            <xsd:simpleType>
                <xsd:list itemType="Name"/>
            </xsd:simpleType>
            <xsd:length value="4"/>
        </xsd:restriction>
    </xsd:simpleType>
    
    <xsd:simpleType name="Name">
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="15" />
            <xsd:pattern value="[a-zA-Z]*" />
        </xsd:restriction>
    </xsd:simpleType>
    
    <!-- *********************************************** -->
    <!-- 5. Same as (4), except the list type is inlined -->
    <!-- *********************************************** -->
    
    <xsd:element name="Names-Inlined" type="Names-Inlined" />
    
    <xsd:simpleType name="Names-Inlined">
        <xsd:restriction>
            <xsd:simpleType>
                <xsd:list>
                    <xsd:simpleType >
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="15" />
                            <xsd:pattern value="[a-zA-Z]*" />
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:list>
            </xsd:simpleType>
            <xsd:length value="4"/>
        </xsd:restriction>
    </xsd:simpleType>
    

    
    <!-- 
        Unions
        6. A union of string and integer
        7. A union of a nonNegativeInteger and a single string
        8. A union of a strings of maxLength 10 and integers of maxInclusive 50
        9. A simpleType that is a synonym for the Names-and-Numbers simpleType
    -->
    
    <!-- ******************************** -->
    <!-- 6. A union of string and integer -->
    <!-- ******************************** -->

    <xsd:element name="Names-and-Numbers" type="Names-and-Numbers" />
    
    <xsd:simpleType name="Names-and-Numbers">
        <xsd:union memberTypes="xsd:string xsd:integer" />
    </xsd:simpleType>
    
    <!-- ****************************************************** -->
    <!-- 7. A union of a nonNegativeInteger and a single string -->
    <!-- ****************************************************** -->
    
    <xsd:element name="maxOccurs" type="maxOccurs" />
    
    <xsd:simpleType name="maxOccurs">
        <xsd:union memberTypes="xsd:nonNegativeInteger" >
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="unbounded" />
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:union>
    </xsd:simpleType>
 
    <!-- *********************************************************************** -->
    <!-- 8. A union of a strings of maxLength 10 and integers of maxInclusive 50 -->
    <!-- *********************************************************************** -->
    
    <xsd:element name="Restricted-Names-and-Numbers" type="Restricted-Names-and-Numbers" />
    
    <xsd:simpleType name="Restricted-Names-and-Numbers">
        <xsd:union>
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:maxLength value="10" />
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType>
                <xsd:restriction base="xsd:integer">
                    <xsd:maxInclusive value="50" />
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:union>
    </xsd:simpleType>

    <!-- ********************************************************************** -->
    <!-- 9. A simpleType that is a synonym for the Names-and-Numbers simpleType -->
    <!-- ********************************************************************** -->
    
    <xsd:element name="My-Names-and-Numbers" type="My-Names-and-Numbers" />
    
    <xsd:simpleType name="My-Names-and-Numbers">
        <xsd:restriction base="Names-and-Numbers" />
    </xsd:simpleType>
    
    <!-- 
        Other
        10. A simpleType restricts a simpleType which restricts another simpleType
        11. Same as (10) except all simpleTypes are inlined
    -->
    
    <!-- ************************************************************************** -->
    <!-- 10. A simpleType restricts a simpleType which restricts another simpleType -->
    <!-- ************************************************************************** -->
    
    <xsd:simpleType name="BostonAreaSurfaceElevation">
        <xsd:restriction base="EarthSurfaceElevation">
            <xsd:minInclusive value="0"/>
            <xsd:maxInclusive value="120"/>
        </xsd:restriction>
    </xsd:simpleType>
    
    <xsd:simpleType name="EarthSurfaceElevation">
        <xsd:restriction base="Elevation">
            <xsd:minInclusive value="-1290"/>
            <xsd:maxInclusive value="29035"/>
        </xsd:restriction>
    </xsd:simpleType>
    
    <xsd:simpleType name="Elevation">
        <xsd:restriction base="xsd:integer">
            <xsd:minInclusive value="-999999"/>
            <xsd:maxInclusive value="999999"/>
        </xsd:restriction>
    </xsd:simpleType>
    
    <!-- *************************************************** -->
    <!-- 11. Same as (10) except all simpleTypes are inlined -->
    <!-- *************************************************** -->
    
    <xsd:simpleType name="BostonAreaSurfaceElevation-Inlined">
        <xsd:restriction>
            <xsd:simpleType>
                <xsd:restriction>
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:integer">
                            <xsd:minInclusive value="-999999"/>
                            <xsd:maxInclusive value="999999"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                    <xsd:minInclusive value="-1290"/>
                    <xsd:maxInclusive value="29035"/>
                </xsd:restriction>
            </xsd:simpleType>	
            <xsd:minInclusive value="0"/>
            <xsd:maxInclusive value="120"/>
        </xsd:restriction>
    </xsd:simpleType>
    
</xsd:schema>
Received on Friday, 24 June 2011 17:54:17 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 24 June 2011 17:54:18 GMT