xml 约束:schema 约束

1、schema约束
   1) 语法:

    1   dtd语法: <!ELEMENT 元素名称 约束>,但是一个xml 只能有一个dtd 约束
    2   schema符合xml的语法,xml语句,一个xml中可以有多个schema

   多个schema使用名称空间区分(类似于java 的类是按照java包名来区分的是一个道理)
   2)数据类型

         XML Schema比XML DTD支持更多的数据类型,并支持用户自定义新的数据类型。

        dtd里面有PCDATA类型,但是在schema里面可以支持更多的数据类型,并且可以自定义
        比如 年龄 只能是整数,在schema可以直接定义一个整数类型
        schema语法更加复杂,结构复杂,不太容易操作,schema目前不能替代dtd

2、schema的快速入门


    创建一个schema文件 后缀名是 .xsd
        根节点 <schema>
    在schema文件里面
        1 属性  xmlns="http://www.w3.org/2001/XMLSchema"
            - 表示当前xml文件是一个约束文件

如何区分,xml 和schema 哪一个是约束呢

  
   写上了这一句就表示是约束文档

       2  targetNamespace="http://www.itcast.cn/20151111" 写网址的目的防止重复
            - 使用schema约束文件,直接通过这个地址引入约束文件
       3 elementFormDefault="qualified" //固定写法
    步骤
        (1)看xml中有多少个元素
            <element>
        (2)看简单元素和复杂元素
            如果复杂元素
                <complexType>
                    <sequence>
                        子元素
                    </sequence>
            </complexType>
        (3)简单元素,写在复杂元素的
            <element name="person">
            <complexType>
            <sequence>
                    <element name="name" type="string"></element>
                    <element name="age" type="int"></element>
            </sequence>
            </complexType>
            </element>

        (4)在被约束文件里面引入约束文件
            <person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://www.itcast.cn/20151111"
            xsi:schemaLocation="http://www.itcast.cn/20151111 1.xsd">

            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                -- 表示xml是一个被约束文件

           xmlns="http://www.itcast.cn/20151111"
                -- 是约束文档里面 targetNamespace
            xsi:schemaLocation="http://www.itcast.cn/20151111 1.xsd">
                -- targetNamespace 空格  约束文档的地址路径
        

xmlns: 表示名称空间,意思就是这个约束来自于含有这个字符串的schema文件

xsi :是别名

 

当随便加标签的时候就报错了,因为里面只能是出现name age  ,以为在schema 文件中只是定义了name,age.

    <sequence>:表示元素的出现的顺序


   

顺序和上面的不一样就报错了

  <all>: 元素只能出现一次
    <choice>:元素只能出现其中的一个
    maxOccurs="unbounded": 表示元素的出现的次数
    <any></any>:表示任意元素

    可以约束属性
        写在复杂元素里面
        写在 </complexType>之前
        --
        <attribute name="id1" type="int" use="required"></attribute>
            - name: 属性名称
            - type:属性类型 int stirng
            - use:属性是否必须出现 required

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.itcast.cn/20151111"
elementFormDefault="qualified">
	<element name="person">
		<complexType>
			<sequence>
			<!-- <all> -->
			<!-- <choice> -->
					<element name="name" type="string" maxOccurs="unbounded"></element>
					<element name="age" type="int"></element>
			<!-- </choice> -->
			<!-- </all>	 -->
			</sequence>
			<attribute name="id1" type="int" use="required"></attribute>
		</complexType>
	</element>
</schema>

 复杂的schema约束

company.xsd 

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.example.org/company" 
elementFormDefault="qualified">
	<element name="company">
		<complexType>
			<sequence>
				<element name="employee">
					<complexType>
						<sequence>
							<!-- 引用任何一个元素 -->
							<any></any>
							<!-- 员工名称 -->
							<element name="name"></element>
						</sequence>
						<!-- 为employee元素添加属性 -->
						<attribute name="age" type="int"></attribute>
					</complexType>
				</element>
			</sequence>
		</complexType>
	</element>
</schema>

department.xsd 

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.example.org/department" 
 elementFormDefault="qualified">
 <!-- 部门名称 -->
 <element name="name" type="int"></element>
</schema>

company.xml 

<?xml version="1.0" encoding="UTF-8"?>
<!-- 数据文件 引用多个Schema -->
<company xmlns = "http://www.example.org/company"//来自于 company.xsd 的约束
	xmlns:dept="http://www.example.org/department"//来自于deparment.xsd 的约束
	xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.example.org/company company.xsd http://www.example.org/department department.xsd" 
>
	<employee age="30">
		<!-- 部门名称 --> 
		<dept:name>100</dept:name>
		<!-- 员工名称 -->
		<name>王晓晓</name>   
	</employee>
</company>


        <company xmlns = "http://www.example.org/company"
    xmlns:dept="http://www.example.org/department"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.org/company company.xsd http://www.example.org/department department.xsd"
>
    引入多个schema文件,可以给每个起一个别名

    <employee age="30">
        <!-- 部门名称 -->
        <dept:name>100</dept:name>
        想要引入部门的约束文件里面的name,使用部门的别名 detp:元素名称
        <!-- 员工名称 -->
        <name>王晓晓</name>   
    </employee>

猜你喜欢

转载自blog.csdn.net/qq_20610631/article/details/81219070