xml element enumerated attribute and enumerated value in xsd

浪尽此生 提交于 2019-12-08 05:18:58

问题


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.



来源:https://stackoverflow.com/questions/11217707/xml-element-enumerated-attribute-and-enumerated-value-in-xsd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!