码迷,mamicode.com
首页 > 其他好文 > 详细

XML(五)-Schema验证

时间:2016-05-17 00:37:41      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:

Schema

什么是Schema

Schema(模式) :其作用与DTD一样,也是用于验证XML文档的有效性,只不过它提供了比DTD更强大的功能和更细粒度的数据类型。另外,Schema可以自定义数据类型。

Schema也是一个XML文件,而DTD则不是。

技术分享

Schema与DTD的比较

  技术分享

为何要Schema

技术分享

Schema文档结构:

 

 

技术分享

所有的Schema文档,其根元素必须叫schema。schema可以包含属性,比如:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">

...
...
</xs:schema>

一个Schema的例子

新建一个dtd文件

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="name" type="xs:string"></xs:element>
    <xs:element name="health" type="xs:int"></xs:element>
    <xs:element name="love" type="xs:int"></xs:element>
    <xs:element name="strain" type="xs:string"></xs:element>
    <xs:group name="mygroup" >
        <xs:sequence>
            <xs:element ref="name"></xs:element>
            <xs:element ref="health"></xs:element>
            <xs:element ref="love"></xs:element>
            <xs:element ref="strain"></xs:element>
        </xs:sequence>
    </xs:group>
  <!--定义一个复合类型--> <xs:complexType name="dogType"> <xs:group ref="mygroup"></xs:group> </xs:complexType> <xs:element name="dog" type="dogType"></xs:element> </xs:schema>

根据以上dtd写的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<dog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\x\Desktop\schema\my3.xsd">
    <name>安倍晋三</name>
    <health>0</health>
    <love>0</love>
    <strain>草狗</strain>
</dog>

Schema的数据类型

基本数据类型

技术分享

扩展数据类型

技术分享

其它数据类型

技术分享

数据类型的约束

技术分享

 

规则

技术分享

Schema的元素类型

技术分享

schema元素:

作用:包含已经定义的schema
用法:<xs:schema>
属性:xmlns                targetNamespace             elementFormDefault           attributeFormDefault
 

Element元素

技术分享

group元素

技术分享

attribute元素

技术分享

attributeGroup元素

技术分享

综合案例:

Schema

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="name" type="xs:string"></xs:element>
    <xs:element name="health" type="xs:int"></xs:element>
    <xs:element name="love" type="xs:int"></xs:element>
    <xs:element name="strain" type="xs:string"></xs:element>
    <xs:group name="mygroup" >
        <xs:sequence>
            <xs:element ref="name"></xs:element>
            <xs:element ref="health"></xs:element>
            <xs:element ref="love"></xs:element>
            <xs:element ref="strain"></xs:element>
        </xs:sequence>
    </xs:group>
    <xs:attribute name="id" type="xs:string"></xs:attribute>
    <xs:attribute name="color" type="xs:string"></xs:attribute>
    <xs:attributeGroup name="myattr">
        <xs:attribute ref="id"></xs:attribute>
        <xs:attribute ref="color"></xs:attribute>
    </xs:attributeGroup>
    <xs:element name="dog">
        <xs:complexType>
                <xs:group ref="mygroup"></xs:group>
                <xs:attributeGroup ref="myattr"></xs:attributeGroup>
        </xs:complexType>
        
    </xs:element>
</xs:schema>

xml

<?xml version="1.0" encoding="UTF-8"?>
<dog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\xx\Desktop\schema\my4.xsd" color="红色" id="id01">
    <name/>
    <health>10</health>
    <love>10</love>
    <strain/>
</dog>

 

XML(五)-Schema验证

标签:

原文地址:http://www.cnblogs.com/nanjingwangbo/p/5500042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!