0
votes

I have following XML schema:

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

    <xs:element name="content" type="contentType"/>

    <xs:complexType name="contentType">
        <xs:complexContent>
            <xs:extension base="versionedElementType">  
                <xs:sequence>   
                    <xs:element name="item" type="itemType" minOccurs="1" maxOccurs="unbounded" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="itemType" mixed="true"> 
        <xs:complexContent>
            <xs:extension base="itemTypeBase">
                    <xs:sequence>   
                        <xs:element name="order" type="xs:unsignedInt"/>
                        <xs:element name="id" type="xs:string"/>
                    </xs:sequence>
            </xs:extension> 
        </xs:complexContent>
    </xs:complexType>

    <!-- Simple type convert to complex type -->
    <xs:complexType name="itemTypeBase" mixed="true">
        <xs:simpleContent>  
            <xs:extension base="itemDescriptionType">
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- Simple type -string restriction -->
    <xs:simpleType name="itemDescriptionType" >
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="64"/>
        </xs:restriction>   
    </xs:simpleType>


    <xs:complexType name="versionedElementType">
        <xs:attribute name="version" type="xs:string" use="required"/>
    </xs:complexType>

</xs:schema>

which I use to validate this XML instance (I want to mix the text in the 'item' element with sub-elements 'order' and 'id'):

<?xml version="1.0" encoding="UTF-8"?>
<content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="Content.xsd"
         version ="1.0">

  <item>Description here...
  <order>2</order>
  <id>2</id>
  </item>  
</content>

Whatever I did the validation still says taht there is an error:

The content type of a derived type and that of its base must both be mixed or both be element-only. Type 'itemType' is mixed, but its base type is not.

But I can see that both types - itemType and itemTypeBase are MIXED!!

Thanks a lot STeN

1

1 Answers

3
votes

First of all the error which I see if I open your schema in Visual Studio 2010 is:

The derived type and the based type must have the same content type.

In you current schema the type itemTypeBase is defined with respect of the <xs:simpleContent> and derived type itemType with the respect of <xs:complexContent> which is not allowed. Either you allow no sub-elements and use <xs:simpleContent> or you do use child elements and use <xs:complexContent>.

I personally don't like and don't use mixed types. If I understand you correct you want to make some restrictions in the text from the content. You want to have the content length between 1 and 64 characters. But <order>2</order>, <id>2</id> and all whitespace, inclusive the new line characters, are also a part of the content. If you want that <item> has simple content, then you can not insert child elements inside.

So the pragmatical solution would be go away from the mixed model and use the XML document in the form

<?xml version="1.0" encoding="utf-8"?>
<content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="Content.xsd"
         version ="1.0">
    <item>
        <description>Description here...</description>
        <order>2</order>
        <id>2</id>
    </item>
</content>

where Content.xsd is

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

    <xs:element name="content" type="contentType"/>

    <xs:complexType name="contentType">
        <xs:sequence>   
            <xs:element name="item" type="itemType"
                        minOccurs="1" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="version" type="xs:string" use="required"/>
    </xs:complexType>

    <xs:complexType name="itemType"> 
        <xs:sequence>
            <xs:element name="description" type="itemDescriptionType"/>
            <xs:element name="order" type="xs:unsignedInt"/>
            <xs:element name="id" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="itemDescriptionType" >
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="64"/>
        </xs:restriction>   
    </xs:simpleType>

</xs:schema>

All will be very simple and clear.