Search code examples
attributesxsdrestrict

XSD : restrict many different attributes that take the same values


I want to do this: XML Schema How to Restrict Attribute by Enumeration

...but I have MANY different attributes that take the exact same values. This makes my XSD very complex. Can I define the restriction once and then reference it on each attribute somehow ???

Thanks in advance. :)


Solution

  • If you have many different attributes that take on the exact same values, simply reuse the attribute type definitions:

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               version="1.0">
    
      <xs:simpleType name="colorType">
        <xs:restriction base="xs:string">
          <xs:enumeration value="red" />
          <xs:enumeration value="green" />
          <xs:enumeration value="blue" />
        </xs:restriction>
      </xs:simpleType>
    
      <xs:element name="RoomColors">
        <xs:complexType>
          <xs:attribute name="wall" type="colorType"/>
          <xs:attribute name="carpet" type="colorType"/>
          <xs:attribute name="ceiling" type="colorType"/>
          <xs:attribute name="funiture" type="colorType"/>
        </xs:complexType>
      </xs:element>  
    
    </xs:schema>
    

    It's when the values are not the exact same that there's more creativity involved. See Extend enumerated lists in XML schema.