schema学习笔记

1.  schema简介

1.1.  schema是什么?

Ø  XML Schema 是基于 XML 的 DTD 替代者。

Ø  XML Schema 可描述 XML 文档的结构。

Ø  XML Schema 语言也可作为 XSD(XML Schema Definition)来引用。

Ø  XML Schema 是 DTD 的继任者

Ø  XML Schema 是 W3C 标准

1.2.  为什么要用schema?

Ø  XML Schema 支持数据类型

Ø  XML Schema 使用 XML 语法

Ø  XML Schema 可保护数据通信

Ø  XML Schema 可扩展

Ø  形式良好是不够的

1.3.  如何使用schema?

1.3.1.  一个xml文件

<?xml version="1.0" encoding="UTF-8"?>

<customer>

         <name>jack</name>

         <address>湖南岳阳</address>

</customer>

1.3.2.  dtd文件

<?xml version="1.0" encoding="UTF-8"?>

<!ELEMENT customer (name,address)>

<!ELEMENT name (#PCDATA)>

<!ELEMENT address (#PCDATA)>

1.3.3.  xsd文件

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

         <xs:element name="customer">

                   <xs:complexType>

                   <xs:sequence>

                            <xs:element  name="name" type="xs:string"></xs:element>

                            <xs:element  name="address" type="xs:string"></xs:element>

                   </xs:sequence>       

                   </xs:complexType>

         </xs:element>

</xs:schema>

1.3.4.  对dtd的引用

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE customer SYSTEM "customer.dtd">

<customer>

         <name>jack</name>

         <address>湖南岳阳</address>

</customer>

1.3.5.  对xsd的引用

<?xml version="1.0" encoding="UTF-8"?>

<customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="customer.xsd">

         <name>jack</name>

         <address>湖南岳阳</address>

</customer>

2.  schema语法

2.1.  schema元素

Ø  xs:schema: 根元素

Ø  xmlns:xs=http://www.w3.org/2001/XMLSchema: 指定所使用的命名空间的id,显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs

Ø  elementFormDefault="qualified"  指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定。

2.2.  在xml文件中引入schema

Ø  如果xsd的schema根元素中指定了targetNamespace="http://www.morris.com"

<?xml version="1.0" encoding="UTF-8"?>

<customer

xmlns=http://www.morris.com   这是对应xsd文件中的targetNamespace

xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance  指定下面schemaLocation的命名空间

xsi:schemaLocation="http://www.morris.com customer.xsd"> 指定xsd文件的位置,注意前面得加上targetNamespace的值

         <name>jack</name>

         <address>湖南岳阳</address>

</customer>

Ø  如果xsd的schema根元素中没有指定了targetNamespace属性

<?xml version="1.0" encoding="UTF-8"?>

<customer

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="customer.xsd">  

         <name>jack</name>

         <address>湖南岳阳</address>

</customer>

2.3.  简易元素

简易元素指那些只包含文本的元素。它不会包含任何其他的元素或属性。它可以是 XML Schema 定义中包括的类型中的一种(布尔、字符串、数据等等),或者它也可以是自行定义的定制类型。也可向数据类型添加限定(即 facets),以此来限制它的内容,或者可以要求数据匹配某种特定的模式。

Ø  语法

<xs:element name="xxx" type="yyy"/>

此处 xxx 指元素的名称,yyy 指元素的数据类型

Ø  常见的数据类型

n  xs:string

n  xs:decimal

n  xs:integer

n  xs:boolean

n  xs:date

n  xs:time

Ø  实例

user.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema

targetNamespace="http://www.morris.com"

xmlns:xs="http://www.w3.org/2001/XMLSchema"

elementFormDefault="qualified" >         

         <xs:element name="user">

                   <xs:complexType>

                            <xs:sequence>

                                     <xs:element name="name" type="xs:string"/>

                                     <xs:element name="age" type="xs:int"/>

                                     <xs:element name="birth" type="xs:date"/>

                            </xs:sequence>

                   </xs:complexType>

         </xs:element>

</xs:schema>

user.xml

<?xml version="1.0" encoding="UTF-8"?>

<user xmlns="http://www.morris.com"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://www.morris.com user.xsd">

         <name>张三</name>

         <age>18</age>

         <birth>1980-12-12</birth>

</user>

2.4.  属性的默认值和固定值

简易元素可拥有指定的默认值或固定值。

当没有其他的值被规定时,默认值就会自动分配给元素。

在下面的例子中,缺省值是 "red":

<xs:element name="color"type="xs:string" default="red"/>

固定值同样会自动分配给元素,并且您无法规定另外一个值。

在下面的例子中,固定值是"red":

<xs:element name="color"type="xs:string" fixed="red"/>

2.5.  属性可选的和必需的属性

在缺省的情况下,属性是可选的。如需规定属性为必选,请使用 "use" 属性:

<xs:attributename="lang" type="xs:string"use="required"/>

2.6.  限定

2.6.1.  对值的限定

限定age元素的值在0到120之间

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

         <xs:element name="age">

         <xs:simpleType>

                   <xs:restriction base="xs:integer">

                            <xs:minInclusive value="0"/>

                            <xs:maxInclusive value="120"/>

                   </xs:restriction>

         </xs:simpleType>    

         </xs:element>

</xs:schema>

2.6.2.  对一组值的限定

如需把 XML 元素的内容限制为一组可接受的值,我们要使用枚举约束(enumeration constraint)。

下面的例子定义了带有一个限定的名为 "car" 的元素。可接受的值只有:Audi, Golf, BMW:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

         <xs:element name="car">

         <xs:simpleType>

                   <xs:restriction base="xs:string">

                            <xs:enumeration value="宝马"/>

                            <xs:enumeration value="奥迪"/>

                            <xs:enumeration value="奔驰"/>

                   </xs:restriction>

         </xs:simpleType>

         </xs:element>

</xs:schema>

2.6.3.  对一组值的限定

如需把 XML 元素的内容限制定义为一系列可使用的数字或字母,我们要使用模式约束(pattern constraint)。

下一个例子定义了带有一个限定的名为 "initials" 的元素。可接受的值是一个字母和一个数字:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

         <xs:element name="initials">

                   <xs:simpleType>

                            <xs:restriction base="xs:string">

                                     <xs:pattern value="[a-zA-Z][0-9]"/>

                            </xs:restriction>

                   </xs:simpleType>

         </xs:element>

</xs:schema>

2.6.4.  对一系列值的其他限定

²  下面的例子定义了带有一个限定的名为 "letter" 的元素。可接受的值是 a - z 中零个或多个字母:

<xs:element name="letter">

<xs:simpleType>

  <xs:restriction base="xs:string">

    <xs:pattern value="([a-z])*"/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

²  下面的例子定义了带有一个限定的名为 "letter" 的元素。可接受的值是一对或多对字母,每对字母由一个小写字母后跟一个大写字母组成。举个例子,"sToP"将会通过这种模式的验证,但是 "Stop"、"STOP" 或者 "stop" 无法通过验证:

<xs:element name="letter">

<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>

2.6.5.  对空白字符的限定

如需规定对空白字符(whitespace characters)的处理方式,我们需要使用 whiteSpace 限定。

whiteSpace 的value属性的取值范围:

Ø  preserve  XML 处理器不会移除任何空白字符

Ø  replace  XML 处理器将移除所有空白字符(换行、回车、空格以及制表符)

Ø  collapse  XML 处理器将移除所有空白字符(换行、回车、空格以及制表符会被替换为空格,开头和结尾的空格会被移除,而多个连续的空格会被缩减为一个单一的空格)

<xs:element name="address">

<xs:simpleType>

  <xs:restriction base="xs:string">

    <xs:whiteSpace value="collapse"/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

2.6.6.  对长度的限定

如需限制元素中值的长度,我们需要使用 length、maxLength 以及 minLength 限定。

length:指定长度

maxLength:指定最大长度

minLength:指定最小长度

<xs:element name="password">

<xs:simpleType>

  <xs:restriction base="xs:string">

    <xs:minLength value="5"/>

    <xs:maxLength value="8"/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

2.6.7.  数据类型的限定

限定

描述

enumeration

定义可接受值的一个列表

fractionDigits

定义所允许的最大的小数位数。必须大于等于0。

length

定义所允许的字符或者列表项目的精确数目。必须大于或等于0。

maxExclusive

定义数值的上限。所允许的值必须小于此值。

maxInclusive

定义数值的上限。所允许的值必须小于或等于此值。

maxLength

定义所允许的字符或者列表项目的最大数目。必须大于或等于0。

minExclusive

定义数值的下限。所允许的值必需大于此值。

minInclusive

定义数值的下限。所允许的值必需大于或等于此值。

minLength

定义所允许的字符或者列表项目的最小数目。必须大于或等于0。

pattern

定义可接受的字符的精确序列。

totalDigits

定义所允许的阿拉伯数字的精确位数。必须大于0。

whiteSpace

定义空白字符(换行、回车、空格以及制表符)的处理方式。

2.7.  空元素

一个空的 XML 元素:

<product prodid="1345" />

schema文件中product元素的声明:

<xs:element name="product">

  <xs:complexType>

    <xs:attribute name="prodid" type="xs:positiveInteger"/>

  </xs:complexType>

</xs:element>

2.8.  仅包含其他元素的元素

xml元素

<person>

<firstname>John</firstname>

<lastname>Smith</lastname>

</person>

schema中的person元素

<xs:element name="person">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="firstname" type="xs:string"/>

      <xs:element name="lastname" type="xs:string"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>

2.9.  仅含文本的元素

此类型仅包含简易的内容(文本和属性),因此我们要向此内容添加 simpleContent 元素。当使用简易内容时,我们就必须在 simpleContent 元素内定义扩展或限定,使用 extension 或 restriction 元素来扩展或限制元素的基本简易类型。

xml元素

<shoesize country="france">35</shoesize>

schema中的shoesize属性

<xs:element name="shoesize">

  <xs:complexType>

    <xs:simpleContent>

      <xs:extension base="xs:integer">

        <xs:attribute name="country" type="xs:string" />

      </xs:extension>

    </xs:simpleContent>

  </xs:complexType>

</xs:element>

2.10.    混合类型

XML 元素,"letter",含有文本以及其他元素:

<letter>

Dear Mr.<name>John Smith</name>.

Your order <orderid>1032</orderid>

will be shipped on <shipdate>2001-07-13</shipdate>.

</letter>

下面这个 schema 声明了这个 "letter" 元素:

<xs:element name="letter">

  <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>

注释:为了使字符数据可以出现在 "letter" 的子元素之间,mixed 属性必须被设置为 "true"。

2.11.    指示器

2.11.1.          Order 指示器

Order 指示器用于定义元素的顺序。分为以下三种:

Ø  All  指示器规定子元素可以按照任意顺序出现,且每个子元素必须只出现一次

Ø  Choice 指示器规定可出现某个子元素或者可出现另外一个子元素(非此即彼)

Ø  Sequence 规定子元素必须按照特定的顺序出现

<xs:element name="person">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="firstname" type="xs:string"/>

      <xs:element name="lastname" type="xs:string"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>

2.11.2.          Occurrence 指示器

Occurrence 指示器用于定义某个元素出现的频率。分为以下两种:

Ø  maxOccurs 指示器指示器可规定某个元素可出现的最大次数

Ø  minOccurs 指示器指示器可规定某个元素可出现的最小次数

<xs:element name="person">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="full_name" type="xs:string"/>

      <xs:element name="child_name" type="xs:string"

      maxOccurs="10" minOccurs="0"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>

如需使某个元素的出现次数不受限制,请使用 maxOccurs="unbounded" 这个声明。

2.11.3.          Group 指示器

Group 指示器用于定义相关的数批元素。

Ø  元素组

<xs:group name="persongroup">

  <xs:sequence>

    <xs:element name="firstname" type="xs:string"/>

    <xs:element name="lastname" type="xs:string"/>

    <xs:element name="birthday" type="xs:date"/>

  </xs:sequence>

</xs:group>

在把 group 定义完毕以后,就可以在另一个定义中引用它了:

<xs:element name="person" type="personinfo"/>

<xs:complexType name="personinfo">

  <xs:sequence>

    <xs:group ref="persongroup"/>

    <xs:element name="country" type="xs:string"/>

  </xs:sequence>

</xs:complexType>

 

Ø  属性组

属性组通过 attributeGroup 声明来进行定义:

下面这个例子定义了名为 "personattrgroup" 的一个属性组:

<xs:attributeGroup name="personattrgroup">

  <xs:attribute name="firstname" type="xs:string"/>

  <xs:attribute name="lastname" type="xs:string"/>

  <xs:attribute name="birthday" type="xs:date"/>

</xs:attributeGroup>

在已定义完毕属性组之后,就可以在另一个定义中引用它了,就像这样:

<xs:attributeGroup name="personattrgroup">

  <xs:attribute name="firstname" type="xs:string"/>

  <xs:attribute name="lastname" type="xs:string"/>

  <xs:attribute name="birthday" type="xs:date"/>

</xs:attributeGroup>

<xs:element name="person">

  <xs:complexType>

    <xs:attributeGroup ref="personattrgroup"/>

  </xs:complexType>

</xs:element>

2.12.    实例

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

         <xs:element name="ContactList">

         <xs:complexType>

                   <xs:sequence>

                                     <xs:element name="Person">

                                               <xs:complexType>

                                                        <xs:sequence>

                                                                 <xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1"/>

                                                                 <xs:element name="Age" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>

                                                        <xs:element name="Gender" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>

                                                        <xs:element name="Email" type="xs:string" minOccurs="0" maxOccurs="unbounded" />

                                                        <xs:element name="Phone" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>

                                                        <xs:element name="Address" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>

                                                        </xs:sequence>

                                               </xs:complexType>

                                     </xs:element>

                   </xs:sequence>

         </xs:complexType>

                  

         </xs:element>

</xs:schema>

 

 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

morris131

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值