Next in thread → Next in month →

Re: [xml-dev] Best practice for XSD enumeration values?

From
Mukul Gandhi <>
To
Kurt Cagle <>
Date
2022-02-17T11:30:06Z
ID
<CANGHzgB4BxKEXbu9wJ7EF5a=XgWOG3Xin5_QX02-7NJCrAN=>
Thread
Re: [xml-dev] Best practice for XSD enumeration values?
Hi Kurt,

On Thu, Feb 17, 2022 at 11:09 AM Kurt Cagle <[email protected]> wrote:
If you had to do this as an enumeration, I'd use the following:
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="airportType">
        <xsd:simpleType>
            <xsd:list>
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="civil"/>
                        <xsd:enumeration value="military"/>
                        <xsd:enumeration value="active"/>
                        <xsd:enumeration value="inactive"/>
                        <xsd:enumeration value="permanent"/>
                        <xsd:enumeration value="temporary"/>
                    </xsd:restriction>                  
                </xsd:simpleType>               
            </xsd:list>
        </xsd:simpleType>
    </xsd:element>
</xsd:schema>
Then you would have:
<airportType>active civil permanent</airportType>

 Nice thought.

The schema you've posted, would allow the XML element <airportType>active active civil permanent</airportType> to be reported as valid (i.e, duplicate strings are permitted).

It seems that, a modification like following (with XSD 1.1 needed) could fix that,

<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   
    <xsd:element name="airportType">
        <xsd:simpleType>
           <xsd:restriction base="AirportType">
              <xsd:assertion test="count($value) = count(distinct-values($value))"/>
           </xsd:restriction>
        </xsd:simpleType>
    </xsd:element>
   
    <xsd:simpleType name="AirportType">
       <xsd:list>
          <xsd:simpleType>
             <xsd:restriction base="xsd:string">
               <xsd:enumeration value="civil"/>
               <xsd:enumeration value="military"/>
               <xsd:enumeration value="active"/>
               <xsd:enumeration value="inactive"/>
               <xsd:enumeration value="permanent"/>
               <xsd:enumeration value="temporary"/>
             </xsd:restriction>                  
          </xsd:simpleType>              
       </xsd:list>
    </xsd:simpleType>
   
</xsd:schema>


--
Regards,
Mukul Gandhi
Next in thread → Next in month →