XML Schema: Complex Types (1)

XML Schema: Complex Types (1)

任意包含参数或者其他元素得元素是复杂类型元素。
例子:
<xsd:element name="shirt">
 <xsd:complexType>
  <xsd:sequence> 
   <xsd:element name="color" type="xsd:string"/>
   <xsd:element name="size" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>
</xsd:element>

//<xsd:sequence> 里的元素只能出现一次,而且要符合顺序

xml文件:
<shirt>
 <color>purple</color>
 <size>XL</size>
</shirt>

设置元素的出现次数
<xsd:element name="completeOrder">
 <xsd:complexType>
  <xsd:sequence>
   <xsd:element name="shirt"  minOccurs="0" maxOccurs="unbounded" type="xsd:string" />
   <xsd:element name="sweatshirt" minOccurs="0" maxOccurs="unbounded" type="xsd:string" />
   <xsd:element name="mugs" minOccurs="0" maxOccurs="100" type="xsd:string" />
   <xsd:element name="hats" minOccurs="0" maxOccurs="50" type="xsd:string" />
  </xsd:sequence>
 </xsd:complexType>
</xsd:element>
使用参数minOccurs="0" 和 maxOccurs="unbounded" 可以控制元素出现的次数
xml文件:
  <completeOrder>
 <shirt>9 purple X</shirt>
        <shirt>3 blue S</shirt>
        <shirt>11 orange XXL</shirt>
 <sweatshirt>19 green</sweatshirt>
        <mugs>8 porcelain</mugs>
  </completeOrder>

比较XML Schema  和 DTD (设置元素出现次数)
XML Schema                       DTD

minOccurs="0"                
maxOccurs="unbounded"            element*

minOccurs="0"                
maxOccurs="1"                    element?

minOccurs="1"                
maxOccurs="unbounded"            element+

Default:
minOccurs="1"                
maxOccurs="1"                    element

学习xsd: choice
<xsd:element name="singleItemOrder">
  <xsd:complexType>
 <xsd:choice>
  <xsd:element name="shirt" type="xsd:string" />
  <xsd:element name="sweatshirt" type="xsd:string"/>
  <xsd:element name="mugs" type="xsd:integer"/>
 </xsd:choice>
  </xsd:complexType>
</xsd:element>
在choice中的元素选项中只能选一个

学习xsd:all
<xsd:element name="completeOrder">
  <xsd:complexType>
 <xsd:all>
  <xsd:element name="shirt" type="xsd:string" minOc-curs="0" maxOccurs="unbounded"/>
  <xsd:element name="sweatshirt" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
  <xsd:element name="mugs" type="xsd:string" minOc-curs="0" maxOccurs="100"/>
         <xsd:element name="hats" type="xsd:string" minOc-curs="0" maxOccurs="50"/>
 </xsd:all>
  </xsd:complexType>
</xsd:element>
在all中的元素不考虑顺序,但是sequence中顺序是固定的

你可能感兴趣的:(XML Schema: Complex Types (1))