PL_Pathum
PL_Pathum

Reputation: 173

How to set attributes for element xsd

how to add attribute for a element which also has restrictions for elements.

                <xs:restriction base="xs:string">
                    <xs:enumeration value="Spring"/>
                    <xs:enumeration value="Summer"/>
                    <xs:enumeration value="Autumn"/>
                    <xs:enumeration value="Fall"/>
                    <xs:enumeration value="Winter"/>
                </xs:restriction>
           <xs:complexType>
               <xs:attribute name="id"/>
           </xs:complexType>
            </xs:simpleType>

i want to set both attribute and restriction for to element. But using this way it's not working even with simpleContent & complexContent. So how to do it ?

Upvotes: 1

Views: 194

Answers (1)

Ghislain Fourny
Ghislain Fourny

Reputation: 7279

You need to do this in two steps:

  • Define a simple type with a restricted value space
  • Define a complex type with simple content that extends this simple type (used for its content) with an attribute

Like so:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      elementFormDefault="qualified">
    <xs:simpleType name="restrictedsimpletype">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Spring"/>
            <xs:enumeration value="Summer"/>
            <xs:enumeration value="Autumn"/>
            <xs:enumeration value="Fall"/>
            <xs:enumeration value="Winter"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:complexType name="restrictedwithattributes">
        <xs:simpleContent>
            <xs:extension base="restrictedsimpletype">
                <xs:attribute name="id" type="xs:string"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:element name="a" type="restrictedwithattributes"/>
</xs:schema>

Upvotes: 2

Related Questions