• webservice03#schema#元素属性定义


    工具软件XMLSpy 2010 破解版,是非常好的写XMl的工具软件.

    1,Schema的好处:

      Schema出现的目的是通过一个更加合理的方式来编写xml的限制文件(基于xml语法的方式);

      Schema可以使用命名空间来支持多个名称相同的元素;

      Schema可以很好的完成对Java或者所有对象的修饰并且提供了大量的数据类型。

    2,Schema文件   .xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns=http://www.w3.org/2001/XMLSchema  <!--Schema的默认命名空间,不能修改,但可以增加前缀,如果增加前缀之后,就意味着所有element等元素都需要增加前缀-->
         targetNamespace=http://www.example.org/01  <!--自己这个文档的命名空间,可以方便其他Schema,xml文件引用 -->
         xmlns:tns=http://www.example.org/01        <!-- 此处的名称和自己的名称空间一致,但增加了tns前缀,此时如果 引用当前文件创建的类型,需要加上tns前缀--> 
          elementFormDefault="qualified">
          
          <!-- user 是一个复杂类型,依次有三个元素 id,userName,birthday -->
          <element name="user">
              <complexType>
                  <sequence>
                      <element name="id" type="int"></element>
                      <element name="userName" type="string"></element>
                      <element name="birthday" type="date"></element>
                  </sequence>
              </complexType>
          </element>
    </schema>

    3, 在xml中引入Schema文件

    <?xml version="1.0" encoding="UTF-8"?>
    
    <user xmlns="http://www.example.org/01"
            xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance  <!-- 创建了一个可以引入其他Schema文件的名称空间 -->
    xsi:schemaLocation="http://www.example.org/01" > <!-- 引入其他名称空间 -->

        <!-- 如果采用文件的方式引入其他命名空间,可以这样: xsi:noNamespaceSchemaLocation="01.xsd" --> <id>1</id> <userName>yangw</userName> <birthday>1992-09-24</birthday> </user>

    特别注意: 如果要在eclipse中使用命名空间的引入,需要为xml增加xml的category

     4, schema元素和属性定义

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="http://www.example.org/01"
             xmlns:tns="http://www.example.org/01" 
             elementFormDefault="qualified">
             <!-- books 是 复杂类型 -->
             <element name="books">
                 <complexType>
                     <!-- sequence 元素必须按照顺序出现-->
                     <!-- maxOccurs最大出现次数,这里用了不限制 -->
                     <sequence  maxOccurs="unbounded">
                         <element name="book">
                             <complexType>
                                 <sequence minOccurs="1" maxOccurs="unbounded">
                                     <element  name="title" type="string" />
                                     <element  name="content" type="string" />
                                     <!--  choice多个选一个 -->
                                     <!--  all 没有顺序,每个元素只能出现一次 -->
                                     <choice>
                                         <element name="author" type="string"/>
                                         <element name="authors">
                                             <complexType>
                                                 <sequence maxOccurs="3">
                                                     <element name="author" type="string" />
                                                 </sequence>
                                             </complexType>
                                         </element>
                                     </choice>
                                 </sequence>
                                 
                                 <!--  属性的定义,必须在sequence后面写 -->
                                 <attribute name="id" type="int" use="required" />
                             </complexType>
                         </element>
                     </sequence>
                 </complexType>
             </element>
    </schema>
    xml例子
    
    <?xml version="1.0" encoding="UTF-8"?>
    <books xmlns="http://www.example.org/01" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.example.org/01 01.xsd ">
      <book>
        <title>Java in action</title>
        <content>my java book</content>
        <author>author1</author>
      </book>
      
      <book>
          <title>English book</title>
          <content>my English book first</content>
          <authors>
              <author>awei</author>
              <author>yangw</author>
          </authors>
      </book>
    </books>

     5, 定义schema文件的几种方式以及优缺点

      1>Russian Doll(俄罗斯玩偶): 只有一个根元素,通过嵌套的方式完成编写

        优点:结构清晰,根元素只有一个

        缺点:元素无法重用. 

        例子:  上面的 “ 4, schema元素和属性定义”

      2>Salami Slice(香肠切片,腊肠切片):

        优点:能够进行最大化的重用

        缺点:根元素不清晰

        例子: 如下,

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="http://www.example.org/01"
             xmlns:tns="http://www.example.org/01" 
             elementFormDefault="qualified">
             
             
             <element name="book" type="tns:bookType" />
             <element name="id" type="int" />
             <element name="title" type="string" />
             <element name="content" type="string" />
             
             <complexType name="bookType">
                 <sequence>
                     <element ref="tns:id" />
                     <element ref="tns:title" />
                     <element ref="tns:content" />
                 </sequence>
             </complexType>
             
    </schema>

      3> Venetian Blind(百叶窗)  通过simpleType完成重用

        优点:一个根节点,结构清晰 

        缺点:

        例子: 如下,

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="http://www.example.org/02"
             xmlns:tns="http://www.example.org/02" 
             elementFormDefault="qualified">
             
             <!-- 根元素person -->
             <element name="person" type="tns:personType" />
         
             <complexType name="personType">
                 <sequence>
                     <element name="name" type="string"/>
                     <element name="age" type="tns:ageType"/>
                     <element name="email" type="tns:emailType" />
                 </sequence>
                 <attribute name="sex" type="tns:sexType" /> 
             </complexType>
             
             <!-- simpleType 出现的目的就是 约束 -->
             <simpleType name="ageType">
                 <restriction base="int">
                     <!-- age 在 [1,150) -->
                     <minInclusive value="1"/>
                     <maxExclusive value="150" />
                 </restriction>
             </simpleType>
             <simpleType name="sexType">
                 <restriction base="string">
                     <!-- 性别是 枚举类型 -->
                     <enumeration value="男" />
                     <enumeration value="女" />
                 </restriction>
             </simpleType>
             <simpleType name="emailType">
                 <restriction base="string">
                     <!-- 基于正则表达式的方式 -->
                     <pattern value="w[wd.]*@[wd.]+.[a-zA-Z]{2,6}"></pattern>
                 </restriction>
             </simpleType>
    </schema>
    <?xml version="1.0" encoding="UTF-8"?>
    <person xmlns="http://www.example.org/02" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.example.org/02 02.xsd "
               sex="男">
          <name>yangw</name>
          <age>15</age>
          <email>yangw@eastcom.com</email>
    </person>

       此外,在一个schema文件中使用另外一个schema文件中的内容

        <include schemaLocation="xxx.xsd"></include> <!-- 在同一个名称空间下可以直接写文件名-->
    ----------- 赠人玫瑰,手有余香     如果本文对您有所帮助,动动手指扫一扫哟   么么哒 -----------


    未经作者 https://www.cnblogs.com/xin1006/ 梦相随1006 同意,不得擅自转载本文,否则后果自负
  • 相关阅读:
    ES6走一波 Generator异步应用
    扁平数据结构化
    Django笔记&教程 6-2 表单(Form)基础操作
    Django笔记&教程 6-3 使用模型(models)创建表单(form)
    Django笔记&教程 6-4 forms进阶操作,重写forms方法
    Django笔记&教程 7-1 基于类的视图(Class-based views)介绍
    Django笔记&教程 7-3 拓展CBVs(Class-based views)
    Django笔记&教程 5-1 基础增删查改
    Django笔记&教程 1-1 一 新建项目
    Django笔记&教程 0-2 框架版本与相关工具
  • 原文地址:https://www.cnblogs.com/xin1006/p/3562009.html
Copyright © 2020-2023  润新知