xml – XSD元素替换组示例?

前端之家收集整理的这篇文章主要介绍了xml – XSD元素替换组示例?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望能够为多种扩展类型指定单选类型.

例如,说我们有海,在海里有很多种鱼.所以在XML中我会写:

<Sea name="Atlantic Ocean">
 <Tuna name="tuna1" />
 <Carp name="carp1" />
 <Carp name="carp2" />
 <Tuna name="tuna2" />
 <Salmon name="salmon1" />
</Sea>

XSD

<xs:complexType name="Fish">
  </xs:complexType>

  <xs:complexType name="Salmon">
    <xs:complexContent>
      <xs:extension base="Fish">
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="Carp">
    <xs:complexContent>
      <xs:extension base="Fish">
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="Tuna">
    <xs:complexContent>
      <xs:extension base="Fish">
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="Sea">
    <xs:complexContent>
      <xs:extension base="GeoZone">
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
          <xs:choice>
            <xs:element type="Fish" name="Fish" minOccurs="0" 
                        maxOccurs="unbounded"/>
          </xs:choice>
        </xs:sequence>
        <xs:attribute name="Name" type="xs:string" use="optional" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

不幸的是,这是无效的,因为我只能添加Fish本身而不是其扩展类型.

使用 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>
原文链接:https://www.f2er.com/xml/293042.html

猜你在找的XML相关文章