XML Tutorial/XML Schema/simpleContent

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

As simple content models

   <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:simpleType name="empty">
   <xs:restriction base="xs:string">
     <xs:enumeration value="" />
   </xs:restriction>
 </xs:simpleType>
 <xs:element name="br">
   <xs:complexType>
     <xs:simpleContent>
       <xs:extension base="empty">
         <xs:attribute name="id" type="xs:ID" />
         <xs:attribute name="class" type="xs:NMTOKEN" />
       </xs:extension>
     </xs:simpleContent>
   </xs:complexType>
 </xs:element>

</xs:schema></source>


Creating Elements with Simple Content and Attributes

   <source lang="xml">

When declaring an element that has simple content, you start with a basic element declaration: <element name="phone">

</element> Within the element declaration, you include a <complexType> declaration in which you specify that you want your element to have simple content. You do this by creating a <complexType> declaration that contains a <simpleContent> element. The <simpleContent> element indicates that the <complexType> cannot contain child elements. It may contain attributes, but otherwise the content will be defined by a simple type. <element name="phone">

   <complexType> 
       <simpleContent> 
            
       </simpleContent> 
   </complexType> 

</element> Within the <simpleContent> element, you can create an <extension> declaration. You must use an <extension> declaration because you will be extending an existing data type by adding attribute declarations. <element name="phone">

 <complexType>
   <simpleContent>
     <extension base="string">
       <attribute name="kind" type="string" default="Home" />
     </extension>
   </simpleContent>
 </complexType>

</element>

Any of the following examples are allowable <phone>elements based on the previous declaration: <phone kind="Home">001-909-555-1212</phone> <phone>001-909-555-1212</phone> <phone /></source>


Defining Elements to Contain Only Text

   <source lang="xml">

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="popType">
   <xsd:simpleContent>
     <xsd:extension base="xsd:integer">
       <xsd:attribute name="year" type="xsd:integer" />
     </xsd:extension>
   </xsd:simpleContent>
 </xsd:complexType>
 <xsd:element name="population" type="popType" />

</xsd:schema>

File: Data.xml

<population xmlns="http://www.wbex.ru" year="1999">445</population></source>


element based on simple type

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <INVENTORY>

  <BOOK InStock="true">
     <TITLE>title 1</TITLE>
     <AUTHOR Born="1835">
        <FIRSTNAME>Mark</FIRSTNAME>
        <LASTNAME>Twain</LASTNAME>
     </AUTHOR>
     <BINDING>hardcover</BINDING>
     <PAGES FrontMatter="8">298</PAGES>
     <PRICE>5.49</PRICE>
     <DATE>2000-03</DATE>
     <ISBN>9-9999-9999-9</ISBN>
  </BOOK>

</INVENTORY>

File: Schema.xsd <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:element name="INVENTORY">
     <xsd:complexType>
        <xsd:sequence>
           <xsd:element name="BOOK" type="BookType" 
              minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
     </xsd:complexType>
  </xsd:element>
  <xsd:complexType name="BookType">
     <xsd:sequence>
        <xsd:element name="TITLE">
           <xsd:complexType mixed="true">
              <xsd:sequence>
                 <xsd:element name="SUBTITLE" type="xsd:string"
                    minOccurs="0" maxOccurs="1"/>
              </xsd:sequence>
           </xsd:complexType>
        </xsd:element>
        <xsd:element name="AUTHOR">
           <xsd:complexType>
              <xsd:sequence>
                 <xsd:element name="FIRSTNAME" type="xsd:string"/>
                 <xsd:element name="LASTNAME" type="xsd:string"/> 
              </xsd:sequence>
              <xsd:attribute name="Born" type="xsd:gYear"/>
           </xsd:complexType>
        </xsd:element>
        <xsd:element name="BINDING">
           <xsd:simpleType>
              <xsd:restriction base="xsd:string">
                 <xsd:enumeration value="hardcover"/>
                 <xsd:enumeration value="mass market paperback"/>
                 <xsd:enumeration value="trade paperback"/>
              </xsd:restriction>
           </xsd:simpleType>
        </xsd:element>
        <xsd:element name="PAGES">
           <xsd:complexType>
              <xsd:simpleContent>
                 <xsd:extension base="xsd:positiveInteger">
                    <xsd:attribute name="FrontMatter" 
                       type="xsd:positiveInteger"/>
                 </xsd:extension>
              </xsd:simpleContent>
           </xsd:complexType>
        </xsd:element>
        <xsd:element name="PRICE">  
           <xsd:simpleType>
              <xsd:restriction base="xsd:decimal">
                 <xsd:minExclusive value="0"/>
                 <xsd:maxExclusive value="100"/>
              </xsd:restriction>
           </xsd:simpleType>
        </xsd:element>
        <xsd:element name="DATE" type="xsd:gYearMonth" minOccurs="0"/>
        <xsd:element name="ISBN">
           <xsd:simpleType>
              <xsd:restriction base="xsd:string">
                 <xsd:pattern value="\d{1}-\d{4}-\d{4}-\d{1}"/>
              </xsd:restriction>
           </xsd:simpleType>
        </xsd:element>
     </xsd:sequence>
     <xsd:attribute name="InStock" type="xsd:boolean" use="required" />   
  </xsd:complexType>

</xsd:schema></source>


The simpleContent element is used if the complex type may only have text content

   <source lang="xml">

The difference between this and a simple type is that it may carry one or more attributes. The only children that are permitted for this element are the restriction and extension 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:element name="Person">
   <xs:complexType>
     <xs:simpleContent>
       <xs:extension base="xs:string">
         <xs:attribute name="age" type="xs:integer" />
       </xs:extension>
     </xs:simpleContent>
   </xs:complexType>
 </xs:element>

</xs:schema></source>