Re: how to constraint sinmpleTypes

Ayalew Kassahun wrote:

> Hi List:
>
> I have (what seams to me) a simple question but I difficulty expressing it
> in my schema. I want to constraint users of my schema to use only a subset
> of the simpleTypes: xsd:int, xsd:double and xsd:string. How can I specify
> that in my schema?
>
> For example in the following document I want the element <age
> type="xsd:short">23</age> be reported as 'invalid' because its content is of
> type short.
>
> <?xml version="1.0"?>
> <purchaseOrder orderDate="1999-10-20">
>     <shipTo country="US">
>         <name>Alice Smith</name>
>         <age type="xsd:short">23</age>
>         <city>Mill Valley</city>
>         <state>CA</state>
>         <zip>90952</zip>
>     </shipTo>
>     <billTo country="US">
>         <name>Robert Smith</name>
>         <street>8 Oak Avenue</street>
>         <city>Old Town</city>
>         <state>PA</state>
>         <zip>95819</zip>
>     </billTo>
> </purchaseOrder>
>
> any suggestion is welcome.
>
> regards
> Ayalew

I'm not exactly sure what you want to do, so here are three solutions.
1. If you want age to _only_ be a short and you want to specify this in the
instance, then you can use the following for the instance:
<purchaceOrder orderDate = "1999-10-20"

xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
<snip/>
<age xsi:type="xsd:short">23</age>
<snip/>

2. If you want age to _only_be a short and you want to specify this in the
schema, then you can use the following in the schema:
<xsd:element name="age" type="xsd:short"/>
where xsd is defined as in case 1.
Now your instance will be valid with <age>23</age> but invalid with <age>hello
world</age>

3. If you want age to be a short OR integer OR double, then in your schema you
use a union
<xsd:element name="age">
  <xsd:simpleType>
    <xsd:union memberTypes="xsd:short xsd:integer xsd:double"/>
  </xsd:simpleType>
</xsd:element>
Now your instance will be valid with <age>23</age> AND <age>23.4</age> but
invalid with <age>hello world</age>

For a better explanation than mine, see the primer [1]

mick.

[1] http://www.w3.org/TR/xmlschema-0/#CreatDt

Received on Tuesday, 30 January 2001 17:10:18 UTC