定义元素的语法:
"xxx" type="yyy"/>
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
这是一些XML元素:
<lastname>Refsneslastname>
<age>36age>
<dateborn>1970-03-27dateborn>
元素的定义:
"lastname" type="xs:string"/>
"age" type="xs:integer"/>
"dateborn" type="xs:date"/>
元素可拥有指定的默认值或固定值。
当没有其他的值被规定时,默认值就会自动分配给元素。
下面的例子中,缺省值是”red”
"color" type="xs:string" default="red"/>
固定值同样会自动分配给元素,并且无法规定另外一个值
"color" type="xs:string" fixed="red"/>
定义属性的语法:
"xxx" type="yyy"/>
在缺省的情况下,属性是可选的。如果规定属性为必选,使用”use”属性
"lang" type="xs:string" use="required"/>
Myfamily.xml
<persons xmlns="http://www.sync.sz"
xmlns:child="http://www.sync.sz.children"
xmlns:gender="http://www.sync.sz.attr"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sync.sz family.xsd
http://www.sync.sz.attr attribute.xsd">
<person>
<firstname>yourfirstname>
<lastname>fatherlastname>
person>
<person>
<firstname>babafirstname>
<lastname>nidelastname>
<child:children>
<child:childname>ssonchild:childname>
child:children>
person>
<person gender:gender="female">
<firstname>yoursfirstname>
<lastname>falastname>
<child:children>
<child:childname>sons1child:childname>
child:children>
person>
persons>
family.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.sync.sz"
elementFormDefault="qualified">
<xs:element name="persons">
<xs:complexType>
<xs:annotation>
<xs:documentation source="description">
any元素使我们有能力通过未被 schema 规定的元素来拓展 XML 文档!
anyAttribute 元素使我们有能力通过未被 schema 规定的属性来扩展 XML 文档!
xs:documentation>
xs:annotation>
<xs:all>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:any minOccurs="0"/>
xs:sequence>
<xs:anyAttribute/>
xs:complexType>
xs:element>
xs:all>
xs:complexType>
xs:element>
xs:schema>
clilden.xsd
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.sync.sz.children"
elementFormDefault="qualified">
<xs:element name="children">
<xs:complexType>
<xs:sequence>
<xs:element name="childname" type="xs:string"
maxOccurs="unbounded"/>
xs:sequence>
xs:complexType>
xs:element>
schema>
attribute.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.sync.sz.attr"
elementFormDefault="qualified">
<xs:attribute name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
xs:restriction>
xs:simpleType>
xs:attribute>
xs:schema>
shiporder_refactor.xml:
<shiporder xmlns="http://www.sync.sz"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sync.sz shiporder_refactor.xsd" orderid="a1">
<orderperson>synczzzorderperson>
<shipto>
<name>aaaname>
<address>shenzhenbaoanquaddress>
<city>shenzhencity>
<country>chinacountry>
shipto>
<item>
<title>abcatitle>
<quantity>12quantity>
<price>99.9price>
item>
<item>
<title>asdtitle>
<quantity>12quantity>
<price>99.9price>
item>
<item>
<title>aasdasdbcatitle>
<quantity>12quantity>
<price>99.9price>
item>
shiporder>
shiporder_refactor.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.sync.sz"
elementFormDefault="qualified">
<xs:simpleType name="stringtype">
<xs:restriction base="xs:string"/>
xs:simpleType>
<xs:simpleType name="inttype">
<xs:restriction base="xs:positiveInteger"/>
xs:simpleType>
<xs:simpleType name="dectype">
<xs:restriction base="xs:decimal"/>
xs:simpleType>
<xs:simpleType name="orderidtype">
<xs:annotation>
<xs:documentation source="description">简单类型元素xs:documentation>
xs:annotation>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
xs:restriction>
xs:simpleType>
<xs:complexType name="shiptotype">
<xs:annotation>
<xs:documentation source="description">复杂类型元素xs:documentation>
xs:annotation>
<xs:sequence>
<xs:element name="name" type="stringtype"/>
<xs:element name="address" type="stringtype"/>
<xs:element name="city" type="stringtype"/>
<xs:element name="country" type="stringtype"/>
xs:sequence>
xs:complexType>
<xs:complexType name="itemtype">
<xs:sequence>
<xs:element name="title" type="stringtype"/>
<xs:element name="note" type="stringtype" minOccurs="0"/>
<xs:element name="quantity" type="inttype"/>
<xs:element name="price" type="dectype"/>
xs:sequence>
xs:complexType>
<xs:attribute name="orderid" type="stringtype">
xs:attribute>
<xs:complexType name="shipordertype">
<xs:annotation>
<xs:documentation source="description">通过type引用复杂类型或者简单类型,ref引用元素属性xs:documentation>
xs:annotation>
<xs:sequence>
<xs:element name="orderperson" type="stringtype" maxOccurs="1" minOccurs="1"/>
<xs:element name="shipto" type="shiptotype" maxOccurs="1" minOccurs="1"/>
<xs:element name="item" type="itemtype" maxOccurs="unbounded" minOccurs="1"/>
xs:sequence>
<xs:attribute ref="orderid" use="required"/>
xs:complexType>
<xs:element name="shiporder" type="shipordertype"/>
xs:schema>