XML/XML Schema/complexContent

Материал из Web эксперт
Версия от 08:26, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

complexContent based on complexType

File: Data.xml
<?xml version="1.0"?>
<products xmlns ="http://www.wbex.ru"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
      <product>
          <description>description 1</description>
          <warranty>warranty</warranty>
          <name>name1</name>
          <image>a.gif</image>
          <cost>1.5</cost>
          <retailer>http://www.wbex.ru</retailer>
      </product>
      <product>
          <description>description 1</description>
          <warranty>lifetime warranty</warranty>
          <name>name2</name>
          <image>b.gif</image>
          <cost>2.00</cost>
          <retailer>http://www.wbex.ru</retailer>
      </product>
</products>
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="warranty" type="xsd:string"/>
    <xsd:element name="guarantee" substitutionGroup="warranty" type="xsd:string"/>
    <xsd:complexType name="appliance">
        <xsd:sequence>
            <xsd:element name="description" type="xsd:string"/>
            <xsd:element ref="warranty" minOccurs="0"/>
        </xsd:sequence>
    </xsd:complexType>
    
    
    <xsd:complexType name="productType">
        <xsd:complexContent>
            <xsd:extension base="appliance">
                <xsd:sequence>
                    <xsd:element name="name" type="xsd:string"/>
                    <xsd:element name="image" type="imageType"/>
                    <xsd:element name="weight" type="xsd:positiveInteger" minOccurs="0"/>
                    <xsd:element name="cost" type="money" maxOccurs="unbounded"/>
                    <xsd:element name="retailer" type="xsd:anyURI"/>
                </xsd:sequence>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>
    <xsd:element name="products">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="product" type="productType" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:simpleType name="money">
        <xsd:restriction base="xsd:decimal">
            <xsd:fractionDigits value="2"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="imageType">
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="(.)+\.(gif|jpg|jpeg|bmp)"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>



Complex content with referenced attribute group

File: Data.xml
<?xml version="1.0"?>
<addr:address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://www.wbex.ru Schema.xsd"
              xmlns:addr="http://www.wbex.ru"
              addr:language="en">
    
</addr:address>
File: Schema.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru"
  xmlns:addr="http://www.wbex.ru"
  attributeFormDefault="qualified"
  elementFormDefault="qualified">
  
 <xsd:element name="address">
  <xsd:complexType mixed="true">
    <xsd:simpleContent>
      <xsd:extension base="xsd:string">
        <xsd:attributeGroup ref="addr:nationality"/>
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>
 </xsd:element>
 <xsd:attributeGroup name="nationality">
    <xsd:attribute name="language" type="xsd:language"/>
 </xsd:attributeGroup>
</xsd:schema>



complexType based on complexContent

File: Data.xml
<?xml version="1.0"?>
<addr:address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://www.wbex.ru Schema.xsd"
              xmlns:addr="http://www.wbex.ru"
              addr:language="en">
  <fullName>
    <first>first</first>
    <middle>middle</middle>
    <last>last</last>
  </fullName>
  <contacts>
    <phone addr:location="home" addr:number="111.222.3333"/>
  </contacts>
</addr:address>
File: Schema.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru"
  xmlns:addr="http://www.wbex.ru"
  attributeFormDefault="qualified">
 <xsd:element name="address">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="fullName">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="first" type="addr:nameType"/>
            <xsd:element name="middle" type="addr:nameType" minOccurs="0"/>
            <xsd:element name="last" type="addr:nameType"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="contacts" type="addr:contactsType" minOccurs="0"/>
    </xsd:sequence>
  <xsd:attributeGroup ref="addr:nationality"/>
  </xsd:complexType>
 </xsd:element>
 
 <xsd:complexType name="nameType">
  <xsd:simpleContent>
    <xsd:extension base="addr:nameString"/>
  </xsd:simpleContent>
 </xsd:complexType>
 
 <xsd:simpleType name="nameString">
  <xsd:restriction base="xsd:string">
    <xsd:maxLength value="50"/>
  </xsd:restriction>
 </xsd:simpleType>
 
  <xsd:complexType name="contactsType">
    <xsd:sequence>
      <xsd:element name="phone" minOccurs="0">
        <xsd:complexType>
          <xsd:complexContent>
            <xsd:restriction base="xsd:anyType">
              <xsd:attribute name="location" type="addr:locationType"/>
              <xsd:attribute name="number" type="xsd:string"/>
            </xsd:restriction>
          </xsd:complexContent>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:simpleType name="locationType">
    <xsd:restriction base="xsd:string"/>
  </xsd:simpleType>
  
 <xsd:attributeGroup name="nationality">
  <xsd:attribute name="language" type="xsd:language"/>
 </xsd:attributeGroup>
</xsd:schema>