-->

xml element enumerated attribute and enumerated va

2019-05-29 08:04发布

问题:

Of interest is the following xml child element:

<optInItem type='MARKETING_EMAILS'>NO</optInItem>

I'd like to enumerate possible values (assume 2 possible values) for attribute 'type' and enumerate possible values for the text value of optInItem (values could be Yes | No). I am starting with the following xsd but am not sure how to add in the two separate enumerations.

 <xs:element name="optInItem" maxOccurs="2" minOccurs="2">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
           <xs:attribute type="xs:string" name="type" use="required"/>
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
</xs:element>

Any suggestions/pointers would be appreciated.

thanks

回答1:

After many iterations, it looks like the following does the trick:

<xs:element name="account">
 <xs:complexType>
  <xs:sequence>
    <xs:element type="optInItemType" name="optInItem" maxOccurs="2" minOccurs="2">
  </xs:sequence>
 </xs:complexType>
</xs:element>
<xs:complexType name="optInItemType"> 
 <xs:simpleContent> 
    <xs:extension base="elementOptInItemType">
         <xs:attribute name="type" type="attrOptInItemType"/> 
    </xs:extension> 
 </xs:simpleContent>
</xs:complexType>  
<xs:simpleType name="elementOptInItemType">
 <xs:restriction base="xs:string">
   <xs:enumeration value="YES"/>
   <xs:enumeration value="NO"/>
 </xs:restriction>
</xs:simpleType>
<xs:simpleType name="attrOptInItemType">
 <xs:restriction base="xs:string">
   <xs:enumeration value="MARKETING_EMAILS"/>
   <xs:enumeration value="UPDATE_NOTIFICATIONS"/>
 </xs:restriction>
</xs:simpleType>

That was a more complicated than I thought it would be. The simpleContent extension base allowed a user defined type and thus was the key to pulling it all together.