XML Schema学习笔记(一)

来源:互联网 发布:买钢琴还是电钢琴 知乎 编辑:程序博客网 时间:2024/04/30 16:54

1Schema

XML Schema是基于XMLDTD替代品

XML Schema描述XML文档的结构

XML Schema语言也指XML Schema Definition(XSD)

XML Schema是一种W3C标准

 

XML Schema

ü         定义可以在文档中出现的element

ü         定义可以在文档中出现的attribute

ü         定义哪些elementchild element

ü         定义child element的顺序

ü         定义child element的数量

ü         定义元素是否为空是否能包含文本

ü         定义elementattribute的数据类型(data type)

ü         定义elementattribute的默认和固定值(default/fix)

 

XML Schemas确保数据的传输

从发送者往接收者发送数据的的关键一点是,双方都必须对内容有相同的预期expectations)。使用XML Schema,发送者将以一种接收者能够知道的方式来描述数据。比如03-11-2004,可能是311也可能是113

<date type=“date”>2004-03-11</date>确保为“YYYY-MM-DD”

 

属性必须用引号括起来

 

一个指向DTDReference

<?xml version="1.0"?>

<!DOCTYPE note SYSTEM "http://www.w3schools.com/dtd/note.dtd">

<note>

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

 

一个指向XML SchemaReference

<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>

</note>

 

<Schema>元素

每一个Schema文件的根元素,常为以下形式:

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified">...

...

</xs:schema>

 

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

表明Schema中使用的elementdata type来自”http://www.w3.org/2001/XMLSchema”命名空间,也制定了所有来自该命名空间的elementdata type都必须加前缀xs

 

targetNameSpace=”http://ww.w3schools.com”

表明被该schema定义的elementnote,to,from,heading,body来自名称空间”http://www.w3schools.com”

 

xmlns=http://ww.w3schools.com

表明默认的命名空间为”http://www.w3scholls.com”

 

elementFormDefault=”qualified”

表明被本schema描述的XML示例文档必须符合名称空间

 

XML文档中指定一个Schema

<?xml version="1.0"?>

<note xmlns="http://www.w3schools.com"

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

xsi:schemaLocation="http://www.w3schools.com note.xsd">

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

 

xmlns=”http://www.w3scholls.com”

指定默认的名称空间

 

一旦让XML Schema实例的名称空间可用

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

则可以使用schemaLocation属性。这个属性有两个值。第一个值是要用的名称空间,第二个是XML Schema的位置。

 

2.简单类型(Simple Types)

2.1 XSD Simple Elements

简单元素是指不包含任何其它子元素和属性的元素,仅仅能包含文本。

 

这里的文本容易引起歧义。文本可以是在XML Schema中定义的其中一种类型(boolean,String,date等等),也可以是自定义类型

 

为了限制文本的内容,也可以为数据类型添加约束(restrictionfacet),或者要求数据符合特定的parttern

 

定义一个简单类型:

<xs:element name=”xxx” type=”yyy”/> xxx名称、yyy类型

 

默认值和固定值

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

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

2.2            XSD Simple Attributes

简单元素不能拥有属性,拥有属性的元素就被认为是一个复合类型。但是属性本身可以被定义为简单类型。

 

<lastname lang=”EN”>smith</lastname>

定义:

<xs:attribute name=”lang” type=”xs:string”/>

 

默认值和固定值

<xs:attribute name=”lang” type=”xs:string” fixed=”EN”/>

<xs:attribute name=”lang” type=”xs:string” default=”EN”/>

 

可选属性

<xs:attribute name=”lang” type=”xs:string” use=”required”/>

 

内容上的约束:

XML元素或者属性被定义了一个数据类型,内容就被添加了约束

比如一个XML元素的类型是”xs:date”并且内容为”Hello World”,则不合法

使用XML Schema,你可以添加自己的约束。这些约束被成为facets

简单类型是也可以添加约束的

2.3            XSD Restrictions/Facets

a.    值上的约束

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

 

b.    一组值的约束

XML元素的内容限制在一组值中

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

也可以采用其它书写方式,这种方式可以使得其它元素也能够使用carType

<xs:element name="car" type="carType"/>

<xs:simpleType name="carType">

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

       <xs:enumeration value="Audi"/>

       <xs:enumeration value="Golf"/>

       <xs:enumeration value="BMW"/>

    </xs:restriction>

</xs:simpleType>

 

c.    一个值串的约束

<xs:element name="letter">

    <xs:simpleType>

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

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

       </xs:restriction>

    </xs:simpleType>

</xs:element>

三个大写字母:<xs:pattern value=”[A-Z][A-Z][A-Z]”/>

三个大或小写字母:<xs:pattern value=”[a-zA-Z][a-zA-Z][a-zA-Z]”/>

xyz中的一个:<xs:pattern value=”[xyz]”/>

五个数字:[0-9][0-9][0-9][0-9][0-9]

d.    值串的其它约束

az的字母可能出现0到多次:pattern value=”([a-z])*”

出现一或多次小写大写字母对:pattern value=”([a-z][A-Z])+”

两者选一:pattern value=”male|female”

必须刚好有8个字符,而起字符必须是小写或大写字母或数字:

 pattern value=”[a-zA-Z0-9]{8}”

 

e.    白空格约束

<xs:element name="address">

    <xs:simpleType>

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

           <xs:whiteSpace value="preserve"/>

       </xs:restriction>

    </xs:simpleType>

</xs:element>

whiteSpace有三个可用值:preservereplacecollapse

preserve:不移除任何白空格

replace: 以空格(space)取代所有的空格字符(line feeds, tabs, spaces, carriage returns)

collapse: 去掉所有的白空格(line feeds, tabs, spaces, carriage returnsspaces替代,行首和行尾空格被去掉,多个spaces被减为一个)

f.    长度约束

lengthmaxLengthminLength

<xs:element name="password">

    <xs:simpleType>

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

           <--xs:length value="5"/-->

           <xs:minLength value="5"/>

           <xs:maxLength value="8"/>

       </xs:restriction>

    </xs:simpleType>

</xs:element>

Restrictions for Datatypes

Constraint

Description

enumeration

Defines a list of acceptable values

fractionDigits

Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero

length

Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero

maxExclusive

Specifies the upper bounds for numeric values (the value must be less than this value)

maxInclusive

Specifies the upper bounds for numeric values (the value must be less than or equal to this value)

maxLength

Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero

minExclusive

Specifies the lower bounds for numeric values (the value must be greater than this value)

minInclusive

Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)

minLength

Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero

pattern

Defines the exact sequence of characters that are acceptable

totalDigits

Specifies the exact number of digits allowed. Must be greater than zero

whiteSpace

Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

 

3.复合类型(Complex Types)

3.1 Complex Elements

一个complex element包括其它elements 或者/ attributes

四种复合类型元素:

ü         空元素 <product pid=”1234”/>

ü         只包含其它元素的元素

ü         仅包含text的元素 <food type=”dessert”>Ice cream</food>

ü         既包含其它元素又包含text的元素

<desc>It happened<date lang=”norweqian”>03.03.99</date></desc>

上面每一种element都可以使用attribute

 

定义复合类型可以利用继承,在已经存在的复合类型的基础上添加一些元素

<xs:element name="employee" type="fullpersoninfo"/>

<xs:complexType name="personinfo">

    <xs:sequence>

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

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

    </xs:sequence>

</xs:complexType>

<xs:complexType name="fullpersoninfo">

    <xs:complexContent>

       <xs:extension base="personinfo">

           <xs:sequence>

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

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

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

           </xs:sequence>

       </xs:extension>

    </xs:complexContent>

</xs:complexType>

 

3.2            Complex Empty Elements

<product prodid=”1345”/>

<xs:element name="product">

    <xs:complexType>

       <xs:complexContent>

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

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

           </xs:restriction>

       </xs:complexContent>

    </xs:complexType>

</xs:element>

也可以

<xs:element name="product">

  <xs:complexType>

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

  </xs:complexType>

</xs:element>

 

3.3            Complex Type – Elements Only

其中的sequence表示元素必须按照固定的顺序出现

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

<xs:complexType name="persontype">

  <xs:sequence>

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

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

  </xs:sequence>

</xs:complexType>

3.4            Complex Text-Only Element

这种类型仅包含简单的内容,因此在内容之外加一层simpleContent元素,在simpleContent元素中必须定义一个extension或者一个restriction

<xs:element name="somename">

  <xs:complexType>

    <xs:simpleContent>

      <xs:extension base="basetype">

        ....

        ....

      </xs:extension>    

    </xs:simpleContent>

  </xs:complexType>

</xs:element>

 

OR

 

<xs:element name="somename">

  <xs:complexType>

    <xs:simpleContent>

      <xs:restriction base="basetype">

        ....

        ....

      </xs:restriction>    

    </xs:simpleContent>

  </xs:complexType>

</xs:element>

例如:

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

对应的Schema

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

当然也可以写成

<xs:element name="shoesize" type="shoetype"/><xs:complexType name="shoetype">

  <xs:simpleContent>

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

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

    </xs:extension>

  </xs:simpleContent>

</xs:complexType>

 

3.5            混合内容的复合类型

<letter>
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.

</letter>

对应的schema

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

或者

<xs:element name="letter" type="lettertype"/>

<xs:complexType name="lettertype" 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>

 

3.6            指示符(Indicators)

顺序指示符(Order Indicators):All,Choice,Sequence

次数指示符(Occurrence Indicators):maxOccurs,minOccurs

群指示符(Group Indicators):Group name, attributeGroup name

3.6.1  Order Indicators:

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>

在使用all指示符的时候,<minOccurs>指示符可以被设置为01<maxOccurs>只能被设置为1

 

Choice其中的一个出现

<xs:element name="person">

  <xs:complexType>

    <xs:choice>

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

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

    </xs:choice>

  </xs:complexType>

</xs:element>

 

sequence指定必须按照特定次序出现

3.6.2        Occurence Indicators

对于所有的“Order”和“Group”指示符而言(any,all,choice,sequence,group name, group reference),默认的minOccursmaxOccurs都是1

要出现无穷多次,可以maxOccurs=”unbounded”

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

 

3.6.3        Group Indicators

Element Groups

group的声明中必须定义一个allchoice或者sequence元素

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

定义了一个组之后,可以在其它定义中指向它

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

 

Attribute Groups

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

3.7            <any> 元素

<any>元素让我们能够以schema没指定的元素去扩展XML文档

比如,下面是family.xsd中的一段

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

</xs:element>

下面是children.xsd中的一段

<?xml version="1.0" encoding="ISO-8859-1"?>

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

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

xmlns="http://www.w3schools.com"

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></xs:schema>

下面是一个合法的XML文件,使用了两个不同的Schema:”family.xsd””children.xsd”

<?xml version="1.0" encoding="ISO-8859-1"?>

<persons xmlns="http://www.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.microsoft.com family.xsd

http://www.w3schools.com children.xsd">

    <person>

       <firstname>Hege</firstname>

       <lastname>Refsnes</lastname>

       <children>

           <childname>Cecilie</childname>

       </children>

    </person>

    <person>

       <firstname>Stale</firstname>

       <lastname>Refsnes</lastname>

    </person>

</persons>

 

3.8            <anyAttribute>元素

<anyAttribute>元素让我们能够以schema所没有定义的attribute去扩展XML文档

family.xsd片段

<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:anyAttribute/>

  </xs:complexType>

</xs:element>

attribute.xsd文件

<?xml version="1.0" encoding="ISO-8859-1"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" 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>

XML文件如下,同时使用family.xsd attribute.xsd

<?xml version="1.0" encoding="ISO-8859-1"?>

<persons xmlns="http://www.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.microsoft.com family.xsd

http://www.w3schools.com attribute.xsd">

    <person gender="female">

       <firstname>Hege</firstname>

       <lastname>Refsnes</lastname>

    </person>

    <person gender="male">

       <firstname>Stale</firstname>

       <lastname>Refsnes</lastname>

    </person>

</persons>

 

3.9            元素替换

假设我们又来自两个国家的用户:英国和挪威,他们既可以使用挪威的元素名又可以使用英国的元素名。

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

<xs:element name="navn" substitutionGroup="name"/>

 

<xs:complexType name="custinfo">

  <xs:sequence>

    <xs:element ref="name"/>

  </xs:sequence>

</xs:complexType>

 

<xs:element name="customer" type="custinfo"/>

<xs:element name="kunde" substitutionGroup="customer"/>

这样,这两个XML都是合法的

<customer>

  <name>John Smith</name>

</customer>

 

OR

 

<kunde>

  <navn>John Smith</navn>

</kunde>

 

禁止元素替换:

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

<xs:element name="navn" substitutionGroup="name"/>

<xs:complexType name="custinfo">

  <xs:sequence>

    <xs:element ref="name"/>

  </xs:sequence>

</xs:complexType>

<xs:element name="customer" type="custinfo" block="substitution"/>

<xs:element name="kunde" substitutionGroup="customer"/>

这样,

<customer>

  <name>John Smith</name>

</customer>

合法,但

<kunde>

  <navn>John Smith</navn>

</kunde>

非法

 

替换的元素必须类型相同或者是该类的子类型。如果而这类型相同,替换元素的类型不必指定。

 

注意:在substitutionGroup(包括head elementsubstitutable element)里面的所有元素必须被指定为global element,否则不生效

 

global element:直接是schema元素的子元素

local element: 内嵌在其它元素中的元素

3.10        XSD例子

 
原创粉丝点击