how to do simple type extension with restriction?

S

scorpion

I have a simple type like this:

<xs:simpleType name="SizeType">
<xs:restriction base="xs:token">
<xs:enumeration value="small"/>
<xs:enumeration value="medium"/>
<xs:enumeration value="large"/>
<xs:enumeration value="xlarge"/>
</xs:restriction>
</xs:simpleType>

Now, I need to have an id attribute to that, so I'm trying to make it a
complex type by extending to

<xs:complexType name="SizeType">
<xs:simpleContent>
<xs:extension base="xs:token">
....
</xs:extension>
</xs:simpleContent>
</xs:complexType>

Now, I'm stuck. How do I change that to a complex type, with the enumeration
there? Better, is there a way to give an id to a simpleType, without
changing it
to a complexType, as the complexType is, er..., complex for such simple
thing?

TIA.
 
M

Martin Honnen

scorpion said:
I have a simple type like this:

<xs:simpleType name="SizeType">
<xs:restriction base="xs:token">
<xs:enumeration value="small"/>
<xs:enumeration value="medium"/>
<xs:enumeration value="large"/>
<xs:enumeration value="xlarge"/>
</xs:restriction>
</xs:simpleType>

Now, I need to have an id attribute to that, so I'm trying to make it a
complex type by extending to

<xs:complexType name="SizeType">
<xs:simpleContent>
<xs:extension base="xs:token">
....
</xs:extension>
</xs:simpleContent>
</xs:complexType>

Now, I'm stuck. How do I change that to a complex type, with the
enumeration
there? Better, is there a way to give an id to a simpleType, without
changing it
to a complexType, as the complexType is, er..., complex for such simple
thing?

As soon as you want an element to have an attribute you need a complex
type for it, you can only use a simple type if the element has only text
data as its content and no child elements and no attributes.
As for your example it is not quite clear what you want to have, if you
want to have an element with its content being of your SizeType and an
additional ID attribute then the schema is


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:simpleType name="SizeType">
<xs:restriction base="xs:token">
<xs:enumeration value="small"/>
<xs:enumeration value="medium"/>
<xs:enumeration value="large"/>
<xs:enumeration value="xlarge"/>
</xs:restriction>
</xs:simpleType>

<xs:complexType name="SizeIdType">
<xs:simpleContent>
<xs:extension base="SizeType">
<xs:attribute name="id" type="xs:ID" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>

<xs:element name="size" type="SizeIdType" />

</xs:schema>

and an example document is

<?xml version="1.0" encoding="UTF-8"?>
<size xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test20040424Xsd.xml"
id="theSize">medium</size>
 
S

scorpion

Martin said:
As soon as you want an element to have an attribute you need a complex
type for it, you can only use a simple type if the element has only text
data as its content and no child elements and no attributes.
As for your example it is not quite clear what you want to have, if you
want to have an element with its content being of your SizeType and an
additional ID attribute then the schema is


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:simpleType name="SizeType">
<xs:restriction base="xs:token">
<xs:enumeration value="small"/>
<xs:enumeration value="medium"/>
<xs:enumeration value="large"/>
<xs:enumeration value="xlarge"/>
</xs:restriction>
</xs:simpleType>

<xs:complexType name="SizeIdType">
<xs:simpleContent>
<xs:extension base="SizeType">
<xs:attribute name="id" type="xs:ID" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>

<xs:element name="size" type="SizeIdType" />

</xs:schema>

and an example document is

<?xml version="1.0" encoding="UTF-8"?>
<size xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test20040424Xsd.xml"
id="theSize">medium</size>

Thanks, I got it.

Is there any reason why attributes are not allowed on simpleType?
There are a lot of cases where I can have very simple schema
if I can have attributes directly on simpleType.
 
M

Martin Honnen

scorpion said:
Is there any reason why attributes are not allowed on simpleType?
There are a lot of cases where I can have very simple schema
if I can have attributes directly on simpleType.

If XML schema only used types to type element content it might make
sense but you can also type attributes and what should be the meaning of
a schema where a simple type is defined which includes an attribute and
then that type is assigned to an attribute elsewhere? Clearly there
needs to be a distinction between types that can be applied to
attributes and elements (which are the simple types) and those types
that can only be applied to elements (which are the complex types)
 
C

C. M. Sperberg-McQueen

scorpion said:
Is there any reason why attributes are not allowed on simpleType?
There are a lot of cases where I can have very simple schema
if I can have attributes directly on simpleType.

If you defined a simple type T as taking the
attributes foo (integer) and bar (string), what
would / should happen when you declare an
attribute as having type T?

I hope this helps.

-C. M. Sperberg-McQueen
World Wide Web Consortium
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top