zoukankan      html  css  js  c++  java
  • Schema指示器(Indicators)

    Schema系列文章

    1、XML对Schema文档的引用123

    2、Schema文档概述123

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

    4、Schema约束

    5、Schema指示器

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

    用指示器(Indicators)我们可以控制文件中元素的使用方法,有七种指示器:

    1、顺序指示器:All、Choice、Sequence;

    2、出现次数指示器:maxOccurs  、minOccurs;

    3、组指示器:Groupname、attributeGroup name;

    • 顺序指示器(Indicators)用于指定元素的顺序

      <all>指示器(Indicators)指明了子元件可以以任何次序出现,并且每个子元件只能出现一次:
      <xs:element  name ="Author">
          <xs:complexType  >
            <xs:all >
              <xs:element  name ="firstname" type ="xs:string"/>
              <xs:element  name ="lastname"  type ="xs:string"/>
            </xs:all>
          </xs:complexType>
        </xs:element>
      
      <choice>指示器(Indicators)指明了随便的子元素都可以出现:在多个子元件里只能选择一个:
      <xs:element  name ="Author">
          <xs:complexType  >
            <xs:choice >
              <xs:element  name ="firstname" type ="xs:string"/>
              <xs:element  name ="lastname"  type ="xs:string"/>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      
      <sequence>指示器(Indicators)指定了子元素必须以一个指明的顺序出现:
      <xs:element  name ="Author">
          <xs:complexType  >
            <xs:sequence >
              <xs:element  name ="firstname" type ="xs:string"/>
              <xs:element  name ="lastname"  type ="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      
    • 出现的次数指示器:

       最多出现次数指示器(Indicators)指明了一个元素可以出现的最多次数;
      最少出现次数指示器(Indicators)指明了一个元素要出现的最小次数:
      <xs:element  name ="Author">
          <xs:complexType  >
            <xs:sequence >
              <xs:element  name ="firstname" type ="xs:string" minOccurs ="0"/>
              <xs:element  name ="lastname"  type ="xs:string" maxOccurs ="2"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      
      tips:为使元件可以重复出现无数次,可以设置maxOccurs="unbounded"的状态;
    • 组指示器(Indicators)用于定义相关的元素组:

      1、元素组:你必须在组声明里定义一个all, choice,或sequence元素。下面的例子定义了一个名为"persongroup"的组:
      <xs:group  name ="persongroup">
          <xs:sequence >
            <xs:element  name ="firstname"/>
            <xs:element  name ="lastname"/>
          </xs:sequence>
        </xs:group>
      
      定义了一个组后,你可以在另一个组参考它,像这样:
      <xs:group  name ="persongroup">
          <xs:sequence >
            <xs:element  name ="firstname"/>
            <xs:element  name ="lastname"/>
          </xs:sequence>
        </xs:group>
      
        <xs:element  name ="person" type ="personinfo"></xs:element>
        <xs:complexType  name ="personinfo">
          <xs:sequence >
            <xs:group  ref ="persongroup"/>
            <xs:element  name ="country"  type ="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      
      2、属性组的声明:
      <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>

            其他:

            <any>元素可以使我们在XML文档中添加没有被schema 定义过的新元素从而扩充XML文档;

            <anyAttribute>元素可使我们在XML文档中添加未被schema指定过的属性;

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

    我们可以在"person"元素的内容里扩充任意元素(在<lastname>的后面);

    请看下面名为"children.xsd"的schema文件:

    <?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文件(叫做"Myfamily.xml"),用上了来自"family.xsd" 和"children.xsd"两篇不同schema的组件

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

    <any> 和<anyAttribute>元素是用于制造可扩展文档的!它们允许文档含有没有在主要XML schema里声明过的其它新元素。

    元素替代:substitutionGroup

    <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>
    
    关闭替代元素:为了防止其他元素被已指定的元素替代(Element Substitution),可以用block属性;
    <xs:element name="name" type="xs:string" block="substitution"/>
    
    关于元素替代的注意事项:
    1、可替代元素类型应和标题元素的类型相同,或是从中派生出来的。如果可替代元素类型和标题元素的类型相同,你就不需要再指明可替代元素的类型了;
    2、可替代元素组里的所有元素(标题元素和可替代元素)必须声明为“全域元素(global element)”,否则它是不会作用的;
    “全域元素”是"schema"元素下面的直接子元素。“本地元素”是嵌套在别的元素里的元素

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

  • 相关阅读:
    JS网页顶部进度条demo
    C# Emit动态代理生成一个实体对象
    C# 表达式树demo
    C# Thread挂起线程和恢复线程
    JS网页加载进度条
    android 布局
    工程发布问题总结
    jquery集锦
    部署maven到服务器
    WebView显示的网页在大分辨率屏下被放大--解决方案
  • 原文地址:https://www.cnblogs.com/LittleFeiHu/p/1894840.html
Copyright © 2011-2022 走看看