XML Tutorial/XML Schema/union

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

By declaring a <union>, you can validate the values in your instance document against multiple types.

   <source lang="xml">

<union memberTypes="whitespace separated list of types"> You specify the types you are combining by including the memberTypes attribute. The value of the memberTypes attribute should be a whitespace-separated list of references to global <simpleType> definitions or built-in XML Schema datatypes. The <union> declaration also allows you to specify your memberTypes by creating local <simpleType> definitions. <simpleType name="UnknownString"

 <restriction base="string"
   <enumeration value="Unknown" />
   </restriction>

</simpleType>

<simpleType name="UnknownOrFloatType"

   <union memberTypes="float contacts:UnknownString"/>

</simpleType> <element name="latitude" type="contacts:UnknownStringOrFloatType"/> <element name="longitude" type="contacts:UnknownStringOrFloatType"/> Some valid elements include the following: <latitude>43.847156</latitude> <longitude>Unknown</longitude> Some invalid elements include these: <latitude>unknown</latitude> <longitude>43.847156 Unknown</longitude></source>


The union method joins the value spaces and lexical spaces of one or more datatypes to derive a new type

   <source lang="xml">

<xsd:element name="recipe">

 <xsd:simpleType>
  <xsd:union>
   <xsd:simpleType>
   <xsd:restriction base="decimal"/>
   </xsd:simpleType>
   <xsd:simpleType>
    <xsd:restriction base="string"/>
   </xsd:simpleType>
  </xsd:union>
 </xsd:simpleType>

</xsd:element>

<recipe>1.5</recipe> <recipe>cups</recipe></source>


When the values of a union datatype are validated, they? validated in the order in which they? defined

   <source lang="xml">

<xsd:element name="dog">

 <xsd:simpleType>
  <xsd:union>
   <xsd:simpleType>
   <xsd:restriction base="nonNegativeInteger"/>
  </xsd:simpleType>
  <xsd:simpleType>
  <xsd:restriction base="string"/>
  </xsd:simpleType>
 </xsd:union>
 </xsd:simpleType>
</xsd:element>

<dog>15</dog> <dog>hound</dog></source>