• Schema之简单元素、复合元素和属性


    Schema系列文章

    1、XML对Schema文档的引用123

    2、Schema文档概述123

    3、Schema之简单元素、复合元素和属性

    4、Schema约束

    5、Schema指示器

    6、如何创建一份XMLSchema And XML Elements

        前面提到schema的三要素:简单元素,复合元素和属性:

    1、简单元素:只含有文本的XML元素。它不可以含有其他元素或属性;

       1.1、为复合元素添加简单元素的两种方式:直接添加方式如下:

    <xs:element  name ="Book">
        <xs:complexType >
          <xs:sequence  >
            <xs:element  name ="title"  type ="xs:string"></xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    

    或者引用的方式:

    <xs:element  name ="Book">
        <xs:complexType >
          <xs:sequence  >
            <xs:element  ref ="title" ></xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    

     1.2、为简单元素设置默认值和固定值:

    <xs:element  name ="price" type ="xs:double"  default ="22.22"></xs:element>
      <xs:element  name ="press" type ="xs:string"  fixed="清华大学出版社"></xs:element>
    

    2、属性:简单的元素没有属性,当一个元素包含属性时,则成为复合元素,

        2.1、属性的定义非常类似于简单元素:

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

         2.2、为复合元素添加属性的方式和添加简单元素的方式类似:

     <xs:attribute  name ="lang" type ="xs:string"></xs:attribute>
    <xs:element  name ="Book">
        <xs:complexType >
          <xs:attribute  ref ="lang" ></xs:attribute>
        </xs:complexType>
      </xs:element>
    
    

    或者:

     <xs:element  name ="Book">
        <xs:complexType >
          <xs:attribute  name ="lang"  type ="xs:string"></xs:attribute>
        </xs:complexType>
      </xs:element>
    

    2.3、同样可以为属性添加默认值或者固定值;

    <xs:attribute  name ="lang" type ="xs:string"  default ="xie"></xs:attribute>
    

    2.4、任意属性和必须属性:必须属性就是说在写XML文档时,该属性必须含有,不是可有可无的;通过use 属性来指定:

    <xs:element  name ="Book">
        <xs:complexType >
          <xs:attribute  name ="lang" type ="xs:string" use ="required"></xs:attribute>
        </xs:complexType>
      </xs:element>
    

    3、复合元素:是含有其他元素和/或属性的XML元素

    复合元素分为四种:空元素、只含有其他元素、只含有文本、含有其他元素和文本;

       3.1、空元素:

    <product pid="1345"/>
    
    空元素的定义:
    方法一: 
    <xs:element  name ="product">
        <xs:complexType >
          <xs:attribute  name ="proid"  type ="xs:positiveInteger"></xs:attribute>
        </xs:complexType>
      </xs:element>
    方法二:
     <xs:element  name ="product"  type ="prodtype"/>
      <xs:complexType  name ="prodtype">
        <xs:attribute  name ="prodid"  type ="xs:positiveInteger"/>
      </xs:complexType>
    
    如果使用方法二,几个元素可以同时引用相同的复合类型;
    3.2、只含有其他元素的复合元素:
    <person>
    <firstname>John</firstname>
    <lastname>Smith</lastname>
    </person>
    

    schema:

    方法一: 
    <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>
    方法二:
    <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.3、只含有简单文本或属性的复合元素:

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

    这种类型只含有简单内容(文本和属性),因此我们在内容周围添加一个simpleContent元素,当用到简单内容时,你必须在simpleContent元素里定义一个扩展或约束,就像这样:

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

    tips:用extension/restriction元素扩展或限制元素的基本简单类型;

         3.4、混合内容的元素:既含有文本又含有其他元素;

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

    上面为了使字符数据能出现在"letter"子元件之间,mixed属性必须设置为"true"。<xs:sequence>标签指出了已定义的元素(name, orderid 和shipdate)在"letter"元素里必须以指定的顺序出现;

    更多参考http://www.cnblogs.com/caoxch/archive/2006/11/17/563856.html

  • 相关阅读:
    编写更好的API
    C,C++语法基础 | 字符串 | 05
    C,C++语法基础 | 变量,表达式与顺序语句 | 01
    业务数据分析 | 深入浅出数据分析入门 | 01
    linux全套 | 目录 | 00
    linux全套 | 组管理和权限管理 | 08
    linux全套 | crond任务调度 | 09
    linux全套 | linux磁盘分区,挂载 | 10
    linux全套 | 网络配置 | 11
    linux全套 | 进程管理 | 12
  • 原文地址:https://www.cnblogs.com/LittleFeiHu/p/1894191.html
Copyright © 2020-2023  润新知