XML Tutorial/XML Schema/complexContent

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

Basing Complex Types on Complex Types

   <source lang="xml">

Declaring an Element of Complex Type 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:complexType name="characteristicsType">
   <xsd:sequence>
     <xsd:element name="weight" type="xsd:string" />
     <xsd:element name="length" type="xsd:string" />
   </xsd:sequence>
     <xsd:attribute name="kind" type="xsd:string" />
 </xsd:complexType>
 <xsd:complexType name="birthType">
   <xsd:complexContent>
     <xsd:extension base="characteristicsType">
       <xsd:sequence>
         <xsd:element name="mother" type="xsd:string" />
         <xsd:element name="birthdate" type="xsd:date" />
       </xsd:sequence>
     </xsd:extension>
   </xsd:complexContent>
 </xsd:complexType>
 <xsd:element name="birth_characteristics" type="birthType" />

</xsd:schema>

File: Data.xml <?xml version="1.0"?> <birth_characteristics xmlns="http://www.wbex.ru" kind="normal">

 <weight>2-3 pounds</weight>
 <length>18-24 inches</length>
 <mother>Abcde</mother>
 <birthdate>1999-06-10</birthdate>

</birth_characteristics></source>


Defining Empty Elements

   <source lang="xml">

Elements that can contain attributes but that have no content between the opening and closing tags are considered empty. 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:complexType name="sourceType">
   <xsd:complexContent>
     <xsd:extension base="xsd:anyType">
       <xsd:attribute name="sectionid" type="xsd:integer" />
       <xsd:attribute name="newspaperid" type="xsd:integer" />
     </xsd:extension>
   </xsd:complexContent>
 </xsd:complexType>
 
 <xsd:element name="source" type="sourceType" />

</xsd:schema>

File: Data.xml <?xml version="1.0"?> <source xmlns="http://www.wbex.ru" sectionid="101" newspaperid="21"/></source>


Derived by Restriction

   <source lang="xml">

<?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:complexType name="ShortAddress">
   <xs:complexContent>
     <xs:restriction base="Address">
       <xs:sequence>
         <xs:element name="Name" type="xs:string" />
         <xs:element name="Street" type="xs:string"
           minOccurs="1" maxOccurs="2" />
         <xs:element name="City" type="xs:string" />
       </xs:sequence>
     </xs:restriction>
   </xs:complexContent>
 </xs:complexType>

</xs:schema></source>