[WebService]之Schema

schema入门

1:schema出现的目的是通过一个更加合理的方式来编写xml文件的限制(以XML语法的方式)

2:schema可以使用命名空间来支持多个名称相同的元素

3:schema可以很好的的完成对JAVA或者所有的对象的修饰并且提供了大量的数据类型

 

<?xml version="1.0" encoding="UTF-8"?>
<!-- schema xmlns:xsd="http://www.w3.org/2001/XMLSchema 表示schema的命名空间,不能改动,为它定义命名空间为xsd,以后的元素必须为xsd才能使用 -->
<!-- targetNamespace="http://www.example.org/01" 被其它地方引用的名称 -->
<!-- xmlns:tns="http://www.example.org/01" 默认命名空间,只能有一个不加前缀 -->
<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="user">
        <complexType>
            <sequence>
                <element name="id" type="int"></element>
                <element name="username" type="string"></element>
                <element name="born" type="date"></element>
            </sequence>
        </complexType>
    </element>
</schema>

验证方式一:

<?xml version="1.0" encoding="UTF-8"?>
<user xmlns="http://www.example.org/01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.org/01">
    <id>1</id>
    <username>admin</username>
    <born>1986-07-20</born>
</user>

验证方式二:

 

<?xml version="1.0" encoding="UTF-8"?>
<user xmlns="http://www.example.org/01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="01.xsd">
    <id>1</id>
    <username>admin</username>
    <born>1986-07-20</born>
</user>

总结:

 

 

 

posted @ 2015-04-27 20:56  superGG  阅读(1135)  评论(0编辑  收藏  举报