XSD元素
定义元素的语法:
- <xs:element name="xxx" type="yyy"/>
Schema常用类型:
- xs:string
- xs:decimal
- xs:integer
- xs:boolean
- xs:date
- xs:time
例子:
这是一些XML元素:
- <lastname>Refsnes</lastname>
- <age>36</age>
- <dateborn>1970-03-27</dateborn>
元素的定义:
- <xs:element name="lastname" type="xs:string"/>
- <xs:element name="age" type="xs:integer"/>
- <xs:element name="dateborn" type="xs:date"/>
元素的默认值和固定值
元素可拥有指定的默认值或固定值。
当没有其他的值被规定时,默认值就会自动分配给元素。
下面的例子中,缺省值是”red”
- <xs:element name="color" type="xs:string" default="red"/>
固定值同样会自动分配给元素,并且无法规定另外一个值
- <xs:element name="color" type="xs:string" fixed="red"/>
XSD属性
定义属性的语法:
- <xs:attribute name="xxx" type="yyy"/>
可选的属性和必须的属性
在缺省的情况下,属性是可选的。如果规定属性为必选,使用”use”属性
- <xs:attribute name="lang" type="xs:string" use="required"/>
XML示例
示例1:
Myfamily.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <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>your</firstname>
- <lastname>father</lastname>
- </person>
-
- <person>
- <firstname>baba</firstname>
- <lastname>nide</lastname>
- <child:children>
- <child:childname>sson</child:childname>
- </child:children>
- </person>
-
- <person gender:gender="female">
- <firstname>yours</firstname>
- <lastname>fa</lastname>
- <child:children>
- <child:childname>sons1</child:childname>
- </child:children>
- </person>
-
-
- </persons>
family.xsd:
- <?xml version="1.0" encoding="UTF-8" ?>
- <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
- <?xml version="1.0" encoding="UTF-8" ?>
- <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:
- <?xml version="1.0" encoding="UTF-8" ?>
- <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>
示例2:
shiporder_refactor.xml:
- <?xml version="1.0" encoding="UTF-8" ?>
-
- <!--xmlns后面直接接uri,使用默认命名空间-->
- <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>synczzz</orderperson>
-
- <shipto>
- <name>aaa</name>
- <address>shenzhenbaoanqu</address>
- <city>shenzhen</city>
- <country>china</country>
- </shipto>
-
- <item>
- <title>abca</title>
- <quantity>12</quantity>
- <price>99.9</price>
- </item>
- <item>
- <title>asd</title>
- <quantity>12</quantity>
- <price>99.9</price>
- </item>
- <item>
- <title>aasdasdbca</title>
- <quantity>12</quantity>
- <price>99.9</price>
- </item>
-
- </shiporder>
shiporder_refactor.xsd:
- <?xml version="1.0" encoding="UTF-8" ?>
- <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>