定義參數(shù)
<xsd:element name="order">
<xsd:complexType>
<xsd:attribute name="shirts" type="xsd:integer"/>
<xsd:attribute name="mugs" type="xsd:integer"/>
<xsd:attribute name="hats" type="xsd:integer"/>
</xsd:complexType>
</xsd:element>
XML文件:
<oeder shirts="9" mugs="3" hats="45">
參數(shù)使用定義
<xsd:attribute name="hats" type="xsd:integer" use="required"/> //必須出現(xiàn)
<xsd:attribute name="hats" type="xsd:integer" use="optional"/> //可選
<xsd:attribute name="hats" type="xsd:integer" use="prohibited"/> //禁止
<xsd:attribute name="hats" type="xsd:integer" use="fixed" value="value"/> //如果有這個(gè)參數(shù),必須是這個(gè)值
<xsd:attribute name="hats" type="xsd:integer" use="default" value="value"/> //如果沒有這個(gè)參數(shù),自動(dòng)加上這個(gè)參數(shù)
參數(shù)和元素
<xsd:element name="order">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="shirts" type="xsd:string"/>
<xsd:element name="sweatshirts" type="xsd:string"/>
<xsd:element name="mugs" type="xsd:string"/>
<xsd:element name="hats" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
<xsd:attribute name="source" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
XML文件:
<order orderDate="2001-04-18" source="cellphone">
<shirt>aaa</shirt>
<sweatshirts>bbb</sweatshirts>
<mugs>ccc</mugs>
<hats>ddd</hats>
</order>
參數(shù)和text
<shirt quantity="4">XL purple</shirt>
<xsd:element name="shirt">
<xsd:complexType>
<xsd:simpleContent>
<xsd:restriction base="xsd:string"> <!-->文本有限制<-->
<xsd:maxLength value="30" />
<xsd:attribute name="quantity" type="xsd:integer"/>
</xsd:restriction>
</xsd:simpleContent> >
</xsd:complexType>
</xsd:element>
<xsd:element name="shirt">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string"> <!-->文本無(wú)限制<-->
<xsd:attribute name="quantity" type="xsd:integer"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
參數(shù) text 嵌入元素
<order orderDate="2001-04-18">
to ship overnight
<shirt>9</shirt>
and
<mugs>7</mugs>
to fairfax
</order>
<xsd:element name="order">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="shirts" type="xsd:string"/>
<xsd:element name="mugs" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
</xsd:element>
用戶自定義
<!-- define simple types -->
<xsd:simpleType name="colorType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="purple" />
<xsd:enumeration value="orange" />
<xsd:enumeration value="blue" />
<xsd:enumeration value="grey" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="sizeType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="M" />
<xsd:enumeration value="L" />
<xsd:enumeration value="XL" />
</xsd:restriction>
</xsd:simpleType>
<!-- define complex type -->
<xsd:complexType name="sizeColorType">
<xsd:sequence>
<xsd:element name="size" type="sizeType" />
<xsd:element name="color" type="colorType" />
</xsd:sequence>
<xsd:attribute name="quantity" type="xsd:integer" />
</xsd:complexType>
使用定義得類型
<!-- define elements -->
<xsd:element name="shirt" type="sizeColorType" />
XML文件:
<shirt quantity="2">
<size>M</size>
<color>blue</color>
</shirt>
關(guān)聯(lián)元素和參數(shù) (重用)
<xsd:element name="shirt" type="xsd:string" />
<xsd:element name="shirt_list">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="shirt" maxOc-curs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:attribute name="quantity" type="xsd:nonNegativeInteger"/>
<xsd:complexType name="quantityAttrType">
<xsd:attribute ref="quantity"/>
</xsd:complexType>
<xsd:element name="mugs" type="quantityAttrType"/>
<xsd:element name="hats" type="quantityAttrType"/>
建立復(fù)雜類型基于已存類型
<!-- define simple types -->
<xsd:simpleType name="colorType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="purple"/>
<xsd:enumeration value="orange"/>
<xsd:enumeration value="blue"/>
<xsd:enumeration value="grey"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="sizeType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="M"/>
<xsd:enumeration value="L"/>
<xsd:enumeration value="XL"/>
</xsd:restriction>
</xsd:simpleType>
<!-- define complex types -->
<xsd:complexType name="sizeColorType">
<xsd:sequence>
<xsd:element name="size" type="sizeType"/>
<xsd:element name="color" type="colorType"/>
</xsd:sequence>
<xsd:attribute ref="quantity"/>
</xsd:complexType>
<xsd:complexType name="shirtDescType">
<xsd:complexContent>
<xsd:extension base="sizeColorType">
<xsd:sequence>
<xsd:element name="material" type="xsd:string" />
<xsd:element name="collar" type="xsd:string" />
<xsd:element name="sleeve" type="xsd:string" />
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<!--define attribute -->
<xsd:attribute name="quantity" type="xsd:nonNegativeInteger"/>
<!-- define element -->
<xsd:element name="shirt" type="shirtDescType"/>
元素分組
<!-- define simple types -->
<xsd:simpleType name="colorType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="purple"/>
<xsd:enumeration value="orange"/>
<xsd:enumeration value="blue"/>
<xsd:enumeration value="grey"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="sizeType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="M"/>
<xsd:enumeration value="L"/>
<xsd:enumeration value="XL"/>
</xsd:restriction>
</xsd:simpleType>
<!-- define group -->
<xsd:group name="sizeColorGroup">
<xsd:sequence>
<xsd:element name="size" type="sizeType"/>
<xsd:element name="color" type="colorType"/>
</xsd:sequence>
</xsd:group>
<!-- define elements -->
<xsd:element name="shirt">
<xsd:complexType>
<xsd:group ref="sizeColorGroup" />
</xsd:complexType>
</xsd:element>
參數(shù)分組
<!-- define simple types -->
<xsd:simpleType name="colorType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="purple"/>
<xsd:enumeration value="orange"/>
<xsd:enumeration value="blue"/>
<xsd:enumeration value="grey"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="sizeType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="M"/>
<xsd:enumeration value="L"/>
<xsd:enumeration value="XL"/>
</xsd:restriction>
</xsd:simpleType>
<!-- define attribute group -->
<xsd:attributeGroup name="clothesAttrGroup">
<xsd:attribute name="quantity" type="xsd:nonNegativeInteger" />
<xsd:attribute name="color" type="colorType" />
<xsd:attribute name="size" type="sizeType" />
<xsd:attribute name="material" type="xsd:string" />
</xsd:attributeGroup>
<!-- define elements -->
<xsd:element name="shirt">
<xsd:complexType>
<xsd:attributeGroup ref="clothesAttrGroup" />
</xsd:complexType>
</xsd:element>
Annotation 和 Documentation
在schema文件中加入注釋
<xsd:annotation>
<xsd:documentation>
text.....
</xsd:documentation>
</xsd:annotation>
include外部xsd文件
1: <xsd:include schemaLocation="filename.xsd" /> //不能重定義
2: <xsd:redefine schemaLocation="shirts.xsd"> //重定義
....
</xsd:redefine>