Question

Suppose i have got schema

 <xs:complexType name="superType">

        <xs:sequence>
            <xs:element type="s:itemType" name="item" maxOccurs="unbounded" minOccurs="1">
            </xs:element>
        </xs:sequence>

        <xs:attribute type="s:stringNameType" name="name" use="required"/>
        <xs:attribute type="s:extraType" name="extra" use="optional" default="ALL"/>

    </xs:complexType>

and my next element:

<xs:complexType name="elegantType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute type="s:stringNameType" name="name" use="required"/>
                <xs:attribute type="s:extraType" name="extra" use="optional" default="ALL"/>

            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

As you can see elegantType and superType have got the same attribute :

<xs:attribute type="s:stringNameType" name="name" use="required"/>
<xs:attribute type="s:extraType" name="extra" use="optional" default="ALL"/>

Its possible to create base type for that element using extension?

I try define base type but i have got error : The content type of a derived type and that of its base must both be mixed or both be element-only. Type 'superType' is element only, but its base type is not.

This is what i try to do :

            <xs:element type="s:itemType" name="item" maxOccurs="unbounded" minOccurs="1">
            </xs:element>
        </xs:sequence>
        </xs:extension>
    </xs:complexContent>


   <!-- <xs:attribute type="s:stringNameType" name="name" use="required"/>
    <xs:attribute type="s:extraType" name="extra" use="optional" default="ALL"/>-->

</xs:complexType>

<xs:complexType name="resourceBase" mixed="true">

    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute type="s:stringNameType" name="name" use="required"/>
            <xs:attribute type="s:extraType" name="extra" use="optional" default="ALL"/>

        </xs:extension>
    </xs:simpleContent>

</xs:complexType>

My problem is that i have got many element and most of them have got the same attribute. And i need create base element

Was it helpful?

Solution

If you have many elements with repeating attributes, you can use declare them in attribute groups and reuse the attributeGroup object in several elements:

In your schema you can declare a group like this:

<xs:attributeGroup name="attributes">
    <xs:attribute type="s:stringNameType" name="name" use="required"/>
    <xs:attribute type="s:extraType" name="extra" use="optional" default="ALL"/>
</xs:attributeGroup>

And then just add it in the place where you would add your attributes:

<xs:complexType name="superType">
    <xs:sequence>...</xs:sequence>
    <xs:attributeGroup ref="attributes"/>
</xs:complexType>

<xs:complexType name="elegantType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attributeGroup ref="attributes"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top