XML Schema学习笔记

本文介绍了 XML Schema 中的数据类型划分、元素与属性的约束机制、类型派生方法及复杂类型的构建方式。涵盖简单类型、复杂类型、列表类型、联合类型及属性组的定义与应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

 

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

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

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

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

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

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

  通过值范围限定:

<xsd:simpleType name="myInteger">
 <xsd:restriction base="xsd:integer">
    <xsd:minInclusive value="10000"/>
    <xsd:maxInclusive value="99999"/>
 </xsd:restriction>
</xsd:simpleType> 

  使用模式匹配限定:

 

<xsd:simpleType name="SKU">
 <xsd:restriction base="xsd:string">
    <xsd:pattern value=""d{3}-[A-Z]{2}"/>
 </xsd:restriction>
</xsd:simpleType>

  使用枚举方式限定:

<xsd:simpleType name="CNCity">
 <xsd:restriction base="xsd:string">
    <xsd:enumeration value="BeiJing"/>
    <xsd:enumeration value="NanChang"/>
    <xsd:enumeration value="ShangHai"/>
 </xsd:restriction>
</xsd:simpleType>
 

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

如:

<xsd:simpleType name="listOfMyIntType">
 <xsd:list itemType="myInteger"/>
</xsd:simpleType>
 

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

<listOfMyInt>20003 15037 95977 95945</listOfMyInt>
 

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

<xsd:simpleType name="USStateList">
      <xsd:list itemType="USState"/>
</xsd:simpleType>

<xsd:simpleType name="SixUSStates">
  <xsd:restriction base="USStateList">
    <xsd:length value="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 value=”100” />
              </xsd:restriction>
      </xsd:simpleType>
</xsd:element>

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

 

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="value"     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="value"     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="value"    type="xsd:decimal"/>
 </xsd:complexType>
</xsd:element> 

13anyType 是所有 Schema 类型的基类型,和 Java 中的 Object 类似。因此,以下定义:

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

  可以写成:

 
 <xsd:element name="anything"/>
 

14Schema 中用 annotationdocumentappInfo 三个元素来进行注释,其中 appIdocument 都是作为 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="value"    type="xsd:decimal"/>
   </xsd:restriction>
 </xsd:complexContent>
 </xsd:complexType>
</xsd:element>
 

 

15choice 仅允许在实例文档中使用其中一个子元素;在 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>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值