zoukankan      html  css  js  c++  java
  • XML Schema学习笔记

    XML Schema学习笔记

     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++中的联合类型有类似的概念,如:


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

  • 相关阅读:
    搭建Hadoop2.6.0+Spark1.1.0集群环境
    分别用Eclipse和IDEA搭建Scala+Spark开发环境
    poj 2586 Y2K Accounting Bug【贪心】【刷题计划】
    poj2109 Power of Cryptography【坑~泪目】【水过】【刷题计划】
    例题6.1 铁轨【算法入门经典】
    第三章 队列【数据结构】【链队列】【循环队列】
    poj 1328 Radar Installation【贪心】【刷题计划】
    hdu 2364 Escape【模拟优先队列】【bfs】
    poj 2965 The Pilots Brothers' refrigerator 【dfs+枚举】【双十一大礼包】
    6.3.3 二叉树重建【算法入门经典】【双十一大礼包】
  • 原文地址:https://www.cnblogs.com/winner/p/601451.html
Copyright © 2011-2022 走看看