xml schema学习笔记

作者:张小根 来源:未知 添加时间:2006年7月3日 字体:

1、复杂类型和简单类型之间最根本的区别就是:复杂类型的内容中可以包含其他元素,也可以带有属性(attribute),但简单类型既不能包含子元素,也不能带有任何属性。



<xsd:complextype name="cnaddress" >



<xsd:sequence>



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



<xsd:element name="street" type="xsd:string"/>



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



<xsd:element name="zip"type="xsd:decimal"/>



</xsd:sequence>



<xsd:attribute name="country" type="xsd:nmtoken" fixed="us"/>



</xsd:complextype>



@



2、element存在约束:element可以通过其minoccurs和maxoccurs两个属性来约束元素实例存在的个数,这两个属性的缺省值都是1,表示默认情况下此元素在xml实例文档中必须出现一次。



3、attribute存在约束:元素属性也可以通过attribute的use属性来约束出现一次或根本不出现;use属性的取值可以是required,optional,prohibited三个值,缺省(默认)值是optional.



4、element和attribute都有一个default和fixed属性,针对element来书,只有当element实例为空时才采用此default值,而attribute是当实例不提供此attribute时才采用此default值,因此对attribute而言,只有其use值是optional时default值才有意义,而且对element和attribute来说fixed和default两个属性不能同时存在,否则会出现错误。



5、直接定义在schema元素下,即schema元素的顶级子元素的element和attribute都是全局的,称之为全局元素和全局属性,你在其他类型定义中可以直接引用。



6、派生新类型有两种方式:第一种就是直接从其他类型中扩展(继承)而来,另外一种就是通过对已有类型进行限定性约束而来。



如:以下有三种通过限定性约束定义的新类型:



通过值范围限定:



<xsd:simpletype name="myinteger">



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



<xsd:mininclusive &#118alue="10000"/>



<xsd:maxinclusive &#118alue="99999"/>



</xsd:restriction>



</xsd:simpletype>



使用模式匹配限定:



<xsd:simpletype name="sku">



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



<xsd:pattern &#118alue="\d{3}-[a-z]{2}"/>



</xsd:restriction>



</xsd:simpletype>



使用枚举方式限定:



<xsd:simpletype name="cncity">



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



<xsd:enumeration &#118alue="beijing"/>



<xsd:enumeration &#118alue="nanchang"/>



<xsd:enumeration &#118alue="shanghai"/>



</xsd:restriction>



</xsd:simpletype>





7、原子类型(不可分割的类型,象string,integer等系统内建的类型)、列表类型、联合类型合起来统一称为简单类型。在schema中有nmtokens、idrefs、entities三种内建的列表类型,你也可以从已有的简单类型来创建list(列表)类型,但你不能从已有的list类型和复杂类型来创建列表(list)类型。



如:



<xsd:simpletype name="listofmyinttype">



<xsd:list itemtype="myinteger"/>



</xsd:simpletype>



在xml实例文档中列表类型的值是通过空格来进行分隔的,如果声明了一个listofmyinttype元素,其值可能是:



<listofmyint>20003 15037 95977 95945</listofmyint>



8、有几个方面的元素可以应用于list类型来进行约束,它们是:length、minlength、maxlength和enumeration,如:



<xsd:simpletype name="usstatelist">



<xsd:list itemtype="usstate"/>



</xsd:simpletype>



<xsd:simpletype name="sixusstates">



<xsd:restriction base="usstatelist">



<xsd:length &#118alue="6"/>



</xsd:restriction>



</xsd:simpletype>



注:针对列表类型要千万注意成员是string类型的,因为string类型中的空格和列表类型的分割符空格会造成部分混淆。



9、对元素的定义可以采用通过指定其type属性为已定义的属性的方式,也可一采用匿名定义类型的方式,如:



采用类型定义:



<xsd:element name=”comment” type=”xsd:string”>



采用匿名定义:



<xsd:element name=”quantity”>



<xsd:simpletype>



<xsd:restriction base=”xsd:positiveinteger”>



<xsd:maxexclusive &#118alue=”100” />



</xsd:restriction>



</xsd:simpletype>



</xsd:element>



10、union(联合)类型表示在xml实例文档中的元素实例符合union类型定义的成员类型中的一种就可以了(合法),这一点和c++中的联合类型有类似的概念,如:



<xsd:simpletype name="addrunion">



<xsd:union membertypes="xsd:string integer"/>



</xsd:simpletype>



11、复杂类型一般可以分为三类:第一类是包含字符内容和属性但不包含子元素;第二类是包含属性和子元素但不包含字符数据(字符数据包含在子元素中);第三类是即包含属性和字符内容又包含子元素的;那么如何来定义这三类类型呢?针对第一类可以通过simplecontent来实现,第二类可以通过complexcontent来做到,第三类只需要将complextype的属性mixed设为true就可以了。具体的例子如下:



第一种类型(从一个简单类型扩展而来,增加了属性):



<xsd:element name="internationalprice">



<xsd:complextype>



<xsd:simplecontent>



<xsd:extension base="xsd:decimal">



<xsd:attribute name="currency" type="xsd:string"/>



</xsd:extension>



</xsd:simplecontent>



</xsd:complextype>



</xsd:element>



第二种类型(有一个element和两个attribute构成):



<xsd:element name="internationalprice">



<xsd:complextype>



<xsd:complexcontent>



<xsd:element name=”country” @type=”xsd:string” />



<xsd:attribute name="currency" type="xsd:string"/>



<xsd:attribute name="&#118alue"@type="xsd:decimal"/>



</xsd:complexcontent>



</xsd:complextype>



</xsd:element>



注意:在这里由于默认情况下缺省是complexcontent,所以在这里简略的写法是:



<xsd:element name="internationalprice">



<xsd:complextype>



<xsd:element name=”country”type=”xsd:string” />

<xsd:attribute name="currency" type="xsd:string"/>



<xsd:attribute name="&#118alue"@type="xsd:decimal"/>



</xsd:complexcontent>



</xsd:element>



第三种类型:



<xsd:element name="letterbody">



<xsd:complextype mixed="true">



<xsd:sequence>



<xsd:element name="salutation">



<xsd:complextype mixed="true">



<xsd:sequence>



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



</xsd:sequence>



</xsd:complextype>



</xsd:element>



<xsd:element name="quantity"type="xsd:positiveinteger"/>



<xsd:element name="productname" type="xsd:string"/>



<xsd:element name="shipdate"type="xsd:date" minoccurs="0"/>



</xsd:sequence>



</xsd:complextype>



</xsd:element>



第三种类型的实例可能如下:



<letterbody>



<salutation>dear mr.<name>robert smith</name>.</salutation>



your order of <quantity>1</quantity> <productname>baby



monitor</productname> shipped from our warehouse on



<shipdate>1999-05-21</shipdate>



</letterbody>



12、根据11的描述那么要定义一个空内容的元素,也就是说定义一个只包含属性的元素,只要在complexcontent中不包含任何子元素,就可以了,如:



<xsd:element name="internationalprice">



<xsd:complextype>



<xsd:attribute name="currency" type="xsd:string"/>



<xsd:attribute name="&#118alue"type="xsd:decimal"/>



</xsd:complextype>



</xsd:element>



13、anytype是所有schema类型的基类型,和java中的object类似。因此,以下定义:



<xsd:element name="anything" type="xsd:anytype"/>



可以写成:



<xsd:element name="anything"/>



14、schema中用annotation、document、appinfo三个元素来进行注释,其中appi和document都是作为annotation的子元素来处理的。并且annotation一般是作为schema的顶层子元素、element的构造、类型定义的顶层子元素的。



如:



<xsd:element name="internationalprice">



<xsd:annotation>



<xsd:documentation xml:lang="en">



element declared with anonymous type



</xsd:documentation>



</xsd:annotation>



<xsd:complextype>



<xsd:annotation>



<xsd:documentation xml:lang="en">



empty anonymous type with 2 attributes



</xsd:documentation>



</xsd:annotation>



<xsd:complexcontent>



<xsd:restriction base="xsd:anytype">



<xsd:attribute name="currency" type="xsd:string"/>



<xsd:attribute name="&#118alue"type="xsd:decimal"/>



</xsd:restriction>



</xsd:complexcontent>



</xsd:complextype>

</xsd:element>



15、choice仅允许在实例文档中使用其中一个子元素;在all中的所有元素都可以出现一次或一次都不出现,并且其中元素实例是没有顺序约束的,而且all必须放在任何内容模型的最顶层,为了说明这个问题,下面先列出一个合法的,然后列出一个不合法的以供对照说明:



<xsd:complextype name="purchaseordertype">



<xsd:all>



<xsd:element name="shipto" type="usaddress"/>



<xsd:element name="billto" type="usaddress"/>



<xsd:element ref="comment" minoccurs="0"/>



<xsd:element name="items"type="items"/>



</xsd:all>



<xsd:attribute name="orderdate" type="xsd:date"/>



</xsd:complextype>



下面是一个不合法的:



<xsd:complextype name="purchaseordertype">



<xsd:sequence>



<xsd:all>



<xsd:element name="shipto" type="usaddress"/>



<xsd:element name="billto" type="usaddress"/>



<xsd:element name="items"type="items"/>



</xsd:all>



<xsd:sequence>



<xsd:element ref="comment" />



</xsd:sequence>



</xsd:sequence>



<xsd:attribute name="orderdate" type="xsd:date"/>



</xsd:complextype>



16、在存在很多类型中都有几个相同的类型的情况,可以采用属性组的方式来进行重用,属性组定义的格式是:



<xsd:attributegroup name=”attrgroupname”>



<xsd:attribute name=”attrname1” type=”xsd:string” />



<xsd:attribute name=”attrname2” type=”xsd:string” />







</xsd:attributegroup>



使用可以采用以下方式:



<xsd:element name=”testattrgroup”>



<xsd:comlextype>



<xsd:element name=”element1”type=”xsd:string” />



<xsd:attributegroup ref=”attrgroupname” />



</xsd:complextype>



</xsd:element>





1、关于include的用法



include元素可以将外部的定义和声明引入到文档中,并且作为新schema文档的一部分,但必须注意的一点是,被包含成员的目标命名空间必须和包含的目标命名空间一样。具体写法例子是:



<include schemalocation=“http://www.example.com/schemas/address.xsd” />



2、如果一个类型是从另一个类型扩展而来的,那么定义为父类型的element,在实例文档中,可以通过符合子类型的element实例来代替,但必须通过xsi:type指明此元素的具体类型。例如:



<xsd:complextype name=”person”>



<xsd:sequence>



<xsd:element name=”firstname” type=”xsd:string” />



<xsd:element name=”lastname” type=”xsd:string” />



</xsd:sequence>



</xsd:complextype>



<!-- 扩展类型定义 -->



<xsd:complextype name=”father”>



<complexcontent>



<xsd:extension base=”person”>



<xsd:sequence>



<xsd:element name=”gender”>



<xsd:restriction base=”string”>



<xsd:enumeration &#118alue=”male” />



</xsd:restriction>



</xsd:element>



</xsd:sequence>



</xsd:extension>



</complexcontent>



</xsd:complextype>



<!-- 类型的声明 -->



<xsd:element name=”human” type=”person” />



在xml实例文档中针对human可以是这样的(和面向对象有类似的概念):



<human xsi:type=”father”>



<name>xiaogen</name>



<gender>male</gender>



</human>



3、关于置换组



xml schema提供了一种机制叫置换组,允许原先定义好的元素被其他元素所替换。更明确的,这个置换组包含了一系列的元素,这个置换组中的每一个元素都被定义为可以替换一个指定的元素,这个指定的元素称为头元素(head element),需要注意的是头元素必须作为全局元素声明,注意,当一个实例文档包含置换元素时替换元素的类型时从它们的头元素那里派生的,此时并不需要使用我们前面所说的xsi:type来识别这些被派生的类型,当定义了置换组之后,并非意味着不能使用头元素,而只能使用这个置换组中的元素,它只是提供了一个允许元素可替换使用的机制。例如:



<xsd:schema>



<xsd:element name=”comment” type=”xsd:string”/>



<xsd:element name=”shipcomment” type=”xsd:string”



@substitutiongroup=”comment” />



<xsd:element name=”customercomment type=”xsd:string” 



substituiongroup=”comment” />



<xsd:element name=”order”>



<xsd:complextype>



<xsd:element name=”productname” type=”xsd:string” />



<xsd:element name=”price” type=”xsd:decimal” />



<xsd:element ref=”comment” />



<xsd:element name=”shipdate” type=”xsd:date” />



</xsd:complextype>



</xsd:element>



</xsd:schema>



下面是实例文档的一个例子:



<order>



<productname>lapis necklace</productname>



<price>999</price>



<shipcomment>use gold wrap if possible</shipcomment>



<customercomment>want this for the holidays!</customercomment>



<shipdate>2004-08-15</shipdate>



</order>



4、抽象元素和抽象类型



当一个元素或者类型被声明为“abstract”时,那么它就不能在实例文档中使用。当一个元素被声明为”abstract”的时候,元素的置换组的成员必须出现在实例文档中。当一个元素相应的类型被定义声明为"abstract"时,所有关联该元素的实例必须使用"xsi:type"来指明一个类型,这个类型必须是非抽象的,同时是在定义中声明的抽象类型的派生类型。



一、如将上面的comment元素的声明更改成:



<xsd:element name=”comment” type=”xsd:string” abstract=”true” />



那么上面的order实例中就只能包含customercomment和shipcomment才是有效的。



二、如果有下面的类型定义:



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



targetnamespace="http://cars.example.com/schema"



xmlns:target="http://cars.example.com/schema";> 



<complextype name="vehicle" abstract="true"/> 



<complextype name="car"> 



<complexcontent>



<extension base="target:vehicle"/>



</complexcontent>



</complextype>



<complextype name="plane"> 



<complexcontent>



<extension base="target:vehicle"/>



</complexcontent>



</complextype>



<element name="transport" type="target:vehicle"/> 



</schema>



根据以上的定义和声明,下面的实例片断就是不能通过验证的:

<transport xmlns="http://cars.example.com/schema"; />

下面经过修改的就可以通过验证了。

<transport xmlns=“http://cars.example.com/schema



@xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"; xsi:type="car"/>



5、为了阻止类型被派生(包括通过限制派生和通过扩展派生),可以使用final来设置(有点和java中的final的概念类似),其值有三个:restriction,extension,#all。在模式文件的根元素schema元素中有一个可选的finaldefault属性,它的值能够取为final属性所允许的几个值之一。指定finaldefault属性的值的效果等于在模式文档中每个类型定义和元素声明中指定final属性,同时其值为finaldefault属性的值。如果想阻止前面的person被限制派生,我们需要修改定义为如下:



<xsd:complextype name=”person” final=”restriction”>



<xsd:sequence>



<xsd:element name=”firstname” type=”xsd:string” />



<xsd:element name=”lastname” type=”xsd:string” />



</xsd:sequence>



</xsd:complextype>



6、因为在实例文档中与父类型关联的元素(也就是声明为父类型的元素)可以通过派生的子类型来替代,这在2中已经有详细的说明和例子。同时,xml schema也提供了一种机制来控制派生类型以及置换组在实例文档中使用的机制。这种机制是通过类型的block属性来控制的,该属性可以取值为:restriction,extension,#all。和final属性一样,在模式文档的根元素schema元素里有一个可选的属性blockdefault,它的值为block属性所允许的值中的一个。指定blockdefault属性的作用等价于在模式文档中为每个类型定义和元素声明指定block属性。如在前面例子中我们想阻止在使用father来替代person的话我们需要修改person的定义如下:



<xsd:complextype name=”person” block=”extension”>



<xsd:sequence>



<xsd:element name=”firstname” type=”xsd:string” />



<xsd:element name=”lastname” type=”xsd:string” />



</xsd:sequence>



</xsd:complextype>



取值为“extension”的block属性将阻止在实例文档中使用通过扩展的派生类型来替换person(即可以阻止father来替换person),然而它不会阻止通过约束的派生来替换person。



7、另外一种派生类型的控制机制是应用于简单类型方面的派生。当定义一个简单类型的时候,我们可以使用fixed属性对它的所有定义的参数进行修饰,以阻止这些参数在类型派生中被修改,例如:



<xsd:simpletype name=”postcode”>



<xsd:restriction base=”xsd:string”>



< xsd:length &#118alue=”7” fixed=”true” />



</xsd:restriction>



</xsd:simpletype>



当这个简单类型被定义之后,我们能够派生出一个新的邮编类型,在其中我们使用了一个没有在基本类型中固定的参数:



<xsd:simpletype name="ukpostcode">



<xsd:restriction base=" postcode">



<xsd:pattern &#118alue="[a-z]{2}\d\s\d[a-z]{2}"/>



</xsd:restriction>



</xsd:simpletype>



然而,我们不能购派生出一个这样的新的邮编类型:在其中我们重新定义了任何在基类型中已经被固定(fixed)的参数:



<xsd:simpletype name="ukpostcode">



<xsd:restriction base="ipo:postcode">



<xsd:pattern &#118alue="[a-z]{2}\d\d[a-z]{2}"/>



<!-- illegal attempt to modify facet fixed in base type --> 



<xsd:length &#118alue="6" fixed="true"/>



</xsd:restriction>



</xsd:simpletype>

ppdesk