XML/XML Schema/union — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:25, 26 мая 2010

A union datatype by reference to existing types

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  <xs:simpleType name="integerOrData">
    <xs:union memberTypes="xs:integer xs:date" />
  </xs:simpleType>
</xs:schema>



A union datatype can embed one or more <xs:simpleType> elements

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  <xs:simpleType name="myIntegerUnion">
    <xs:union>
      <xs:simpleType>
        <xs:restriction base="xs:integer" />
      </xs:simpleType>
      <xs:simpleType>
        <xs:restriction base="xs:NMTOKEN">
          <xs:enumeration value="undefined" />
        </xs:restriction>
      </xs:simpleType>
    </xs:union>
  </xs:simpleType>
</xs:schema>



The order between derivation by list and derivation by union depends on the result to achieve

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  <xs:simpleType name="listOfUnions">
    <xs:list>
      <xs:simpleType>
        <xs:union memberTypes="xs:date xs:integer" />
      </xs:simpleType>
    </xs:list>
  </xs:simpleType>
  <xs:simpleType name="UnionOfLists">
    <xs:union>
      <xs:simpleType>
        <xs:list itemType="xs:date" />
      </xs:simpleType>
      <xs:simpleType>
        <xs:list itemType="xs:integer" />
      </xs:simpleType>
    </xs:union>
  </xs:simpleType>

</xs:schema>



Union anonymous simple types

File: Data.xml
<?xml version="1.0"?>
<Party xmlns="http://www.wbex.ru"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation=
                        "http://www.wbex.ru
                         Schema.xsd">
        <Participants>
            <Name>Item0</Name>
            <Name>Item1</Name>
            <Name>Item2</Name>
        </Participants>
</Party>
File: Schema.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wbex.ru"
            xmlns="http://www.wbex.ru"
            elementFormDefault="qualified">
    <xsd:simpleType name="CostelloFamily">
        <xsd:union>
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="Item0"/>
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="Item1"/>
                    <xsd:enumeration value="Item2"/>
                    <xsd:enumeration value="Item3"/>
                    <xsd:enumeration value="Item4"/>
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="Item5"/>
                    <xsd:enumeration value="Item6"/>
                    <xsd:enumeration value="Item7"/>
                    <xsd:enumeration value="Item8"/>
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="Item9"/>
                    <xsd:enumeration value="Item10"/>
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="Item11"/>
                    <xsd:enumeration value="Item12"/>
                    <xsd:enumeration value="Item13"/>
                    <xsd:enumeration value="Item14"/>
                    <xsd:enumeration value="Item15"/>
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="Item16"/>
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="Item17"/>
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:union>
    </xsd:simpleType>
    <xsd:element name="Party">
        <xsd:complexType>
             <xsd:sequence>
                 <xsd:element name="Participants">
                     <xsd:complexType>
                         <xsd:sequence>
                             <xsd:element name="Name" type="CostelloFamily" maxOccurs="unbounded"/>
                         </xsd:sequence>
                     </xsd:complexType>
                 </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



union types with restriction

File: Data.xml
<?xml version="1.0"?>
<Party xmlns="http://www.wbex.ru"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
        <Participants>
            <Name>Item0</Name>
            <Name>Item1</Name>
        </Participants>
</Party>

File: Schema.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wbex.ru"
            xmlns="http://www.wbex.ru"
            elementFormDefault="qualified">
    <xsd:simpleType name="Group0">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="Item0"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="Group1">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="Item1"/>
            <xsd:enumeration value="Item2"/>
            <xsd:enumeration value="Item3"/>
            <xsd:enumeration value="Item4"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="Group2">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="Item5"/>
            <xsd:enumeration value="Item6"/>
            <xsd:enumeration value="Item7"/>
            <xsd:enumeration value="Item8"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="Group3">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="Item9"/>
            <xsd:enumeration value="Item10"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="Group4">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="Item11"/>
            <xsd:enumeration value="Item12"/>
            <xsd:enumeration value="Item13"/>
            <xsd:enumeration value="Item14"/>
            <xsd:enumeration value="Item15"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="Group5">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="Item16"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="Group6">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="Item17"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="CostelloFamily">
        <xsd:union memberTypes="Group0 Group1 Group2 Group3 
                                Group4 Group5 Group6"/>
    </xsd:simpleType>
    <xsd:element name="Party">
        <xsd:complexType>
             <xsd:sequence>
                 <xsd:element name="Participants">
                     <xsd:complexType>
                         <xsd:sequence>
                             <xsd:element name="Name" type="CostelloFamily" maxOccurs="unbounded"/>
                         </xsd:sequence>
                     </xsd:complexType>
                 </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



union with memberTypes

File: Data.xml
<?xml version="1.0"?>
<schema xmlns="http://www.wbex.ru"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation=
                    "http://www.wbex.ru
                     maxOccurs.xsd">
        <element maxOccurs="unbounded"/>
</schema>

File: maxOccurs.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.wbex.ru"
        xmlns="http://www.wbex.ru"
        elementFormDefault="qualified">
    <xsd:simpleType name="unbounded_type">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="unbounded"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="maxOccurs_type">
        <xsd:union memberTypes="unbounded_type xsd:nonNegativeInteger"/>
    </xsd:simpleType>
    <xsd:element name="schema">
        <xsd:complexType>
             <xsd:sequence>
                 <xsd:element name="element">
                     <xsd:complexType>
                         <xsd:attribute name="maxOccurs" type="maxOccurs_type" default="1"/>
                     </xsd:complexType>
                 </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>