XML/XML Schema/choice

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

choice minOccurs="0" maxOccurs="unbounded"

File: Data.xml
<?xml version="1.0"?>
<employees xmlns="http://www.wbex.ru/employees"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.wbex.ru/employees Schema.xsd">
  <employee>
    <name>
      <first>first</first>
      <middle>middle</middle>
      <last>last</last>
    </name>
    <location>
      <address>USA</address>
      <latitude>123</latitude>
      <longitude>123</longitude>
    </location>
    <phone>001</phone>
    <knows />
    <description>
      test
      <em>em</em>
      test
      <br />
      test
      <strong>strong</strong>
      test
    </description>
  </employee>
</employees>
File: Schema.xsd
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
  xmlns:employees="http://www.wbex.ru/employees"
  targetNamespace="http://www.wbex.ru/employees"
  elementFormDefault="qualified">
  <element name="employees">
    <complexType>
      <sequence>
        <element name="employee" minOccurs="0"
          maxOccurs="unbounded">
          <complexType>
            <sequence>
              <element name="name"
                type="employees:NameType" />
              <element name="location"
                type="employees:LocationType" />
              <element name="phone" type="string" />
              <element name="knows"
                type="employees:KnowsType" />
              <element name="description"
                type="employees:DescriptionType" />
            </sequence>
          </complexType>
        </element>
      </sequence>
    </complexType>
  </element>
  <complexType name="NameType">
    <group ref="employees:NameGroup" />
  </complexType>
  <group name="NameGroup">
    <sequence>
      <element name="first" type="string" minOccurs="1"
        maxOccurs="unbounded" />
      <element name="middle" type="string" minOccurs="0"
        maxOccurs="1" />
      <element name="last" type="string" />
    </sequence>
  </group>
  <complexType name="LocationType">
    <choice minOccurs="0" maxOccurs="unbounded">
      <element name="address" type="string" />
      <sequence>
        <element name="latitude" type="string" />
        <element name="longitude" type="string" />
      </sequence>
    </choice>
  </complexType>
  <complexType name="KnowsType"></complexType>
  <complexType name="DescriptionType" mixed="true">
    <choice minOccurs="0" maxOccurs="unbounded">
      <element name="em" type="string" />
      <element name="strong" type="string" />
      <element name="br" type="string" />
    </choice>
  </complexType>
</schema>



Choice with three elements

File: Data.xml
<?xml version="1.0"?>
<airport xmlns="http://www.wbex.ru"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation= "http://www.wbex.ru Schema.xsd">
        <plane>data</plane>
</airport>
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:element name="airport">
        <xsd:complexType>
            <xsd:choice>
                <xsd:element name="train" type="xsd:string"/>
                <xsd:element name="plane" type="xsd:string"/>
                <xsd:element name="automobile" type="xsd:string"/>
            </xsd:choice>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



complexType mixed="true" and choice

File: Data.xml
<?xml version="1.0" standalone="yes"?>
<Recipe xmlns:xsi="http://www.w3.org/2001/10/XMLSchema-instance">
  <title>title</title>
  <Body>
    text
    <ingredient>inner</ingredient>
    test
    <ingredient>inner</ingredient>
    after
  </Body>
</Recipe>

File: Schema.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified">
  <xsd:element name="Recipe">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="title" />
        <xsd:element ref="Body" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="title" type="xsd:string">
  </xsd:element>
  <xsd:element name="Body">
    <xsd:complexType mixed="true">
      <xsd:choice>
        <xsd:element ref="ingredient" minOccurs="0"
          maxOccurs="unbounded" />
      </xsd:choice>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="ingredient" type="xsd:string">
  </xsd:element>
</xsd:schema>



Use choice to define elements with random number and occurence

File: Data.xml
<?xml version="1.0"?>
<binary-string xmlns="http://www.wbex.ru"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
        <one/>
        <one>1</one>
        <zero/>
        <one>1</one>
        <zero>0</zero>
</binary-string>
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:element name="binary-string">
        <xsd:complexType>
            <xsd:choice minOccurs="0" maxOccurs="unbounded">
                <xsd:element name="zero" type="xsd:unsignedByte" fixed="0"/>
                <xsd:element name="one" type="xsd:unsignedByte" fixed="1"/>
            </xsd:choice>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>