【webservice】-schema

Schema

 

定义user.xsd的schema文件定义了接下来编写xml的规范

<?xml version="1.0"encoding="UTF-8"?>

<schema xmlns="http://www.w3.org/2001/XMLSchema"

     targetNamespace="http://www.example.org/user"

     xmlns:tns="http://www.example.org/user"

     elementFormDefault="qualified">

     <element name="user">

        <complexType>

          <sequence>

     <element name="id"type="int"></element>

     <element name="name"type="string"></element>

     <element name="sex"type="string"></element>

          </sequence>

        </complexType>

     </element>

</schema>

如上所示是schema文件,定义了如下所示的user.xml的生成规范,如下所示我们可以通过下面的方式引入自定义的schema文件

<?xml version="1.0"encoding="UTF-8"?>

<user xmlns="http://www.example.org/user"

     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

     xsi:schemaLocation="http://www.example.org/user">

    <id>12</id>

    <name></name>

    <sex></sex>

</user>

如图所示我们把命名空间引进来以后,在编写xml的时候,就会自动提示了。

 

如下所示是第二个schema片段

Book.dtd

<?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">
         <elementname="books">
                   <complexType>
                   <!--maxOccurs表示最大出现次数 -->
                            <sequencemaxOccurs="unbounded">
                                     <elementname="book">
                                     <!--complexType复杂类型 表示一个对象-->
                                               <complexType>
                                                    <!--minOccurs 表示最小出现次数-->
                                                        <sequenceminOccurs="1" maxOccurs="unbounded">
                                                                 <elementname="title" type="string" />
                                                                 <elementname="content" type="string" />
                                                                 <choice>
                                                                           <elementname="author" type="string" />
                                                                           <elementname="authors">
                                                                                    <complexType>
                                                                                             <all><!--每个元素只能出现一次内部元素可以无序-->
                                                                                                       <elementname="author" type="string"/>
                                                                                             </all>
                                                                                    </complexType>
                                                                           </element>
                                                                 </choice>
                                                                 <!--sequence内部元素有序出现-->
                                                        </sequence>
                            <!--attribute必须在sequence之后属性的定义 表示book 必须有id属性 -->
                                                        <attributename="id" type="int" use="required"/>
                                               </complexType>
                                     </element>
                            </sequence>
                   </complexType>
         </element>
</schema>

 

如下代码片段所示是按照上面的schema的描述生成的book.xml

<?xml version="1.0"encoding="UTF-8"?>
<book:booksxmlns:book="http://www.example.org/02"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="02.xsd">
         <book:bookid="1">
                   <book:title>Javain action</book:title>
                   <book:content>Javais good</book:content>
                   <book:author>Bruce</book:author>
         </book:book>
         <book:bookid="2">
                   <book:title>SOAin action</book:title>
                   <book:content>soais difficult</book:content>
                   <book:authors>
                       <book:author>Jike</book:author>
                   </book:authors>
         </book:book>
</book:books>

如下所示是

<?xml version="1.0"encoding="UTF-8"?>
<xsd:schemaxmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   targetNamespace="http://www.example.org/classroom"
                   xmlns:tns="http://www.example.org/classroom"
                   elementFormDefault="qualified">
         <xsd:elementname="student" type="tns:studentType"/>
         <xsd:complexTypename="studentType">
                   <xsd:sequence>
                            <xsd:elementname="name" type="xsd:string"/>
                            <xsd:elementname="sex" type="tns:sexType"/>
                   </xsd:sequence>
         </xsd:complexType>
        
         <xsd:simpleTypename="sexType">
                   <xsd:restrictionbase="xsd:string">
                            <xsd:enumerationvalue="男"/>
                            <xsd:enumerationvalue="女"/>
                   </xsd:restriction>
         </xsd:simpleType>
</xsd:schema>

如上所示的schema中simpleTyle是可以自定义并且复用的,restriction中有严格的性别限制,是对一个定义的严格限制complexType定义复杂类型,也就是对象类型

Dtd的互相引用与导入:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schemaxmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   targetNamespace="http://www.example.org/classroom"
                   xmlns:tns="http://www.example.org/classroom"
                   elementFormDefault="qualified">
         <xsd:include schemaLocation="student.xsd"/>
         <xsd:elementname="classroom" type="tns:classroomType"/>
         <xsd:complexTypename="classroomType">
                   <xsd:sequence>
                            <xsd:elementname="grade" type="tns:gradeType"/>
                            <xsd:elementname="name" type="xsd:string"/>
                 <xsd:elementname="stus">
                                     <xsd:complexType>
                                               <xsd:sequenceminOccurs="1" maxOccurs="unbounded">
                                                       <xsd:elementname="student" type="tns:studentType"/>
                                               </xsd:sequence>
                                     </xsd:complexType>
                            </xsd:element>
                   </xsd:sequence>
         </xsd:complexType>
         <xsd:simpleTypename="gradeType">
                   <xsd:restrictionbase="xsd:int">
                            <xsd:minInclusivevalue="2000"/>
                            <xsd:maxInclusivevalue="3000"/>
                   </xsd:restriction>
         </xsd:simpleType>
</xsd:schema>

如上所示定义的是一个班级类型,这其中可以看到<xsd:include schemaLocation="student.xsd"/>引用了student.xsd的schema文件,也就是说这个文件可以引用student.xsd的schema文件,并复用simpleType类型。

总结所用到的标签有

xsd:schema   xsd是schema的名空间

xmlns:tns     tns是当前文件的名空间

simpleType   简单类型的定义,可以进行复用加入限制条件

minInclusive   最小值

maxInclusive   最大值

restriction     限制

sequence     顺序排列的元素序列

element      元素类型

xsd:include   元素引用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值