梦相随1006

版权归 梦相随1006 所有,未经 https://www.cnblogs.com/xin1006 作者许可,严禁转载

导航

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[\w\d\.]*@[\w\d\.]+\.[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> <!-- 在同一个名称空间下可以直接写文件名-->

 

posted on 2014-02-23 15:39  梦相随1006  阅读(407)  评论(0编辑  收藏  举报