XSD element substitution group example?

前端 未结 1 1142
青春惊慌失措
青春惊慌失措 2020-12-05 20:48

I\'d like to be able to specify single choice type for multiple extending types.

For example, say we have the sea, in the sea there are many kinds of fishes. So in

相关标签:
1条回答
  • 2020-12-05 21:27

    Use an element substitution group...

    XSD

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
      <xs:element name="Sea">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="FishSubGroup"
                        minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
          <xs:attribute name="name" type="xs:string" use="optional"/>
        </xs:complexType>
      </xs:element>
    
      <xs:element name="FishSubGroup" abstract="true"/>
    
      <xs:element name="Tuna" type="FishType" substitutionGroup="FishSubGroup"/>
      <xs:element name="Carp" type="FishType" substitutionGroup="FishSubGroup"/>
      <xs:element name="Salmon" type="FishType" substitutionGroup="FishSubGroup"/>
    
      <xs:complexType name="FishType">
        <xs:attribute name="name" type="xs:string"/>
      </xs:complexType>
    </xs:schema>
    

    XML

    <?xml version="1.0" encoding="UTF-8"?>
    <Sea name="Atlantic Ocean">
      <Tuna name="tuna1"/>
      <Carp name="carp1"/>
      <Carp name="carp2"/>
      <Tuna name="tuna2"/>
      <Salmon name="salmon1"/>
    </Sea>
    
    0 讨论(0)
提交回复
热议问题