下面对于XSD的介绍,会有对应的解释和例子,一目了然。
<?xml version="1.0" encoding="UTF-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3school.com.cn" xmlns="http://www.w3school.com.cn" elementFormDefault="qualified"> <xs:element name="note"> <xs:complexType> <xs:sequence> <!-- 一.简单类型的元素的一些介绍 --> <!--这个是缺省值--> <xs:element name="to" type="xs:string" default="red"/> <!--这个是固定值,无法被xml文件覆盖--> <xs:element name="from" type="xs:decimal" fixed="123.00"/> <!--整数形式 对范围进行限定,0到120--> <xs:element name="heading"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction> </xs:simpleType> </xs:element> <!--日期格式--> <xs:element name="body" type="xs:date"/> <!-- 对一组值的限定 如需把 XML 元素的内容限制为一组可接受的值,我们要使用枚举约束(enumeration constraint)。 下面的例子定义了带有一个限定的名为 "car" 的元素。可接受的值只有:AUDI, GOLF, BMW: --> <xs:element name="car"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="AUDI"/> <xs:enumeration value="GOLF"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType> </xs:element> <!-- 对一系列值的限定 如需把 XML 元素的内容限制定义为一系列可使用的数字或字母,我们要使用模式约束(pattern constraint)。 下面的例子定义了带有一个限定的名为 "letter" 的元素。可接受的值只有小写字母 a - z 其中的一个: --> <xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <!--只有一个字母--> <xs:pattern value="[a-z]"/> <!--这个是三个字母--> <!--<xs:pattern value="[a-z][a-z][a-z]"/>--> </xs:restriction> </xs:simpleType> </xs:element> <!-- 带用限定名是“choice”的元素,可接受的值是字母x,y或z的一个 --> <xs:element name="choice"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[xyz]"/> </xs:restriction> </xs:simpleType> </xs:element> <!-- 限定名为“one”的元素,可接受的值是a-z中的零个或多个字母 --> <xs:element name="one"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-z])*"/> </xs:restriction> </xs:simpleType> </xs:element> <!-- 限定名为“two”的元素,可接受的是一对或者多对字母,每队字母是一个小写后面跟着一个大写,比如:sToP --> <xs:element name="two"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-z][A-Z])+"/> </xs:restriction> </xs:simpleType> </xs:element> <!-- 限定名是gender,可接受的值是male或者是female。 --> <xs:element name="gender"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="male|female"/> </xs:restriction> </xs:simpleType> </xs:element> <!-- 限定名是password,可接受的值是由8个字符组成的一行字符,这些字符必须是大写或者小写字符a-z亦或数字0-9 --> <xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z0-9]{8}"/> </xs:restriction> </xs:simpleType> </xs:element> <!-- 如需规定对空白字符(whitespace characters)的处理方式,我们需要使用 whiteSpace 限定。 下面的例子定义了带有一个限定的名为 "address" 的元素。这个 whiteSpace 限定被设置为 "preserve", 这意味着 XML 处理器不会移除任何空白字符: 如果whiteSpace的值是replace,则XML处理器将移除所有空白字符。 如果whiteSpace的值是collapse,这意味着 XML 处理器将移除所有空白字符(换行、回车、空格以及制表符会被替换为空格, 开头和结尾的空格会被移除,而多个连续的空格会被缩减为一个单一的空格)。 --> <xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="preserve"/> </xs:restriction> </xs:simpleType> </xs:element> <!-- 限定名是leng的元素,长度的精确限定是8个字符。 --> <xs:element name="leng"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="8"/> </xs:restriction> </xs:simpleType> </xs:element> <!-- 限定名是mleng,长度是介于5-8之间 --> <xs:element name="mleng"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="5"/> <xs:maxLength value="8"/> </xs:restriction> </xs:simpleType> </xs:element> <!-- 以下是关于限定词的一些总结: enumeration:定义可接受的一个列表, fractionDigits:定义所允许的最大的小数位数,必须大于等于0, length:定义所允许的字符或者列表项目的精确数目。必须大于或等于0 maxExclusive:定义数值的上限,所允许的值必须小于此值 maxInclusive:定义数值的上限,所允许的值必须小于或者等于此值 maxLength:定义所允许的字符或者列表项目的最大数目,必须大于或等于0 minExclusive:定义数值的下限,所允许的值必须大于此值 minInclusive:定义数值的下限,所允许的值必须大于或等于此值 patten:定义可接受的字符的精确序列 totalDigits:定义所允许的阿拉伯数字的精确位数,必须大于0 whiteSpace:定义空白字符(换行,回车,空格以及制表符)的处理方式 --> <!-- 二.复杂类型的元素的一些介绍 --> <!-- 复合元素的类型: 1.空元素 2.包含其他元素的元素 3.仅包含文本的元素 4.包含元素和文本的元素 复合元素的一些例子: 1.复合元素,"product",是空的 <product pid="1345"/> 2.复合元素,"employee",仅包含其他元素 <employee> <firstname>John</firstname> <lastname>Smith</lastname> </employee> 3.复合元素,"food",仅包含文本 <food type="dessert">Ice cream</food> 4.复合元素,"description",包含元素和文本 <description> It happened on <date lang="norwegian">03.03.99</date> </description> --> <!--仅包含其他元素的元素--> <xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <!--空元素--> <xs:element name="product"> <xs:complexType> <xs:complexContent> <xs:restriction base="xs:integer"> <xs:attribute name="pid" type="xs:positiveInteger"/> </xs:restriction> </xs:complexContent> </xs:complexType> </xs:element> <!-- 仅包含简单文本 <shoesize country="france">35</shoesize> --> <xs:element name="shoesize"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="country" type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <!-- 混合元素 <letters> Dear Mr.<name>John Smith</name>. Your order <orderid>1032</orderid> will be shipped on <shipdate>2001-07-13</shipdate>. </letters> --> <xs:element name="letters"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="orderid" type="xs:positiveInteger"/> <xs:element name="shipdate" type="xs:date"/> </xs:sequence> </xs:complexType> </xs:element> <!-- 三.XSD指示器 --> <!-- XSD的指示器有七种: 一.Order指示器(定义元素的顺序) All Choice Sequence 二.Occurrence maxOccurs minOccurs 三.Group Group name attributeGroup name --> <!-- All指示器: <all>指示器规定子元素可以按照任意顺序出现,且每个子元素必须值出现一次 --> <xs:element name="person"> <xs:complexType> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all> </xs:complexType> </xs:element> <!-- Choice指示器 <choice>指示器规定可出现某个子元素或者可出现另外一个子元素 --> <xs:element name="person2"> <xs:complexType> <xs:choice> <xs:element name="employee" type="xs:string"/> <xs:element name="member" type="xs:string"/> </xs:choice> </xs:complexType> </xs:element> <!-- Sequence指示器 <sequence>指示器规定子元素必须按照特定的顺序出现 --> <xs:element name="person3"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <!-- Occurrence 指示器 Occurrence 指示器用于定义某个元素出现的频率。 注释:对于所有的 "Order" 和 "Group" 指示器(any、all、choice、sequence、group name 以及 group reference), 其中的 maxOccurs 以及 minOccurs 的默认值均为 1。 --> <!-- maxOccurs 指示器 <maxOccurs>指示器可规定某个元素可出现的最大次数 如果需要使每个元素的出现不受限制,请使用maxOccurs="unbounded"这个声明。 minOccurs 指示器 <minOccurs> 指示器可规定某个元素能够出现的最小次数: --> <xs:element name="person4"> <xs:complexType> <xs:sequence> <xs:element name="fullname" type="xs:string"/> <xs:element name="childname" type="xs:string" maxOccurs="10" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> <!-- Group指示器 Group指示器用于定义相关的数批元素。 //TODO 定义和使用暂时不明 --> <!-- <any> 元素使我们有能力通过未被 schema 规定的元素来拓展 XML 文档! <any>中的元素可以在外部其他的XSD文档中定义。 --> <xs:element name="person5"> <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:complexType> </xs:element> <!-- <anyAttribute>元素使我们有能力通过未被schema规定的属性来扩展XML文档。 --> <xs:element name="person6"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> <xs:anyAttribute/> </xs:complexType> </xs:element> <!-- 数值类型 在这里我就不一一介绍了,查看手册就ok了。 --> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>