XML_schema约束

引入schema

1.填写xml文档的根元素
2.引入xsi前缀. xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
3.引入xsd文件命名空间. xsi:schemaLocation=“http://www.itcast.cn/xml student.xsd”
4.为每一个xsd约束声明一个前缀,作为标识 xmlns:a="http://www.itcast.cn/xml"设置
前缀为a不写a代表空前缀, xmlns=“http://www.itcast.cn/xml”

自定义student.xsd

<?xml version="1.0"?>
<xsd:schema xmlns="http://www.itcast.cn/xml"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.itcast.cn/xml" elementFormDefault="qualified">
    <xsd:element name="students" type="studentsType"/>
    <xsd:complexType name="studentsType">
        <xsd:sequence>
            <xsd:element name="student" type="studentType" min0ccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="studentType">
        <xsd:sequence>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="age" type="ageType" />
            <xsd:element name="sex" type="sexType" />
        </xsd:sequence>
        <xsd:attribute name="number" type="numberType" use="required"/>
    </xsd:complexType>
    <xsd:simpleType name="sexType">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="male"/>
            <xsd:enumeration value="female"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="ageType">
        <xsd:restriction base="xsd:integer">
            <xsd:minInclusive value="0"/>
            <xsd:maxInclusive value="256"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="numberType">
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="heima_\d{4}"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema> 

自定义student.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!-- 
	1.填写xml文档的根元素
	2.引入xsi前缀.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	3.引入xsd文件命名空间.  xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd"
	4.为每一个xsd约束声明一个前缀,作为标识  xmlns="http://www.itcast.cn/xml" 
 -->
 <students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 	xmlns="http://www.itcast.cn/xml" 
 	xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd">
 	<student number="heima_0001">
 		<name>tom</name>
 		<age>18</age>
 		<sex>male</sex>
 	</student>
 </students>




以下是带注释的(内容同上)
············



解析:带注释的自定义student.xsd()

<?xml version="1.0"?>
<xsd:schema xmlns="http://www.itcast.cn/xml"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.itcast.cn/xml" elementFormDefault="qualified">
    <!--定义一个元素students,元素类型studentsType(自定义类型)-->
    <xsd:element name="students" type="studentsType"/>
    <xsd:complexType name="studentsType">
        <xsd:sequence><!--按顺序出现student元素-->
            <!--
                name="student":又定义一个student
                type="studentType"元素类型:studentType元素
                minOccurs="0":最少出现0次
                maxOccurs="unbounded":最多不限制
            -->
            <xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="studentType"><!--studentType复合类型-->
        <xsd:sequence><!--按顺序出现,name,age,sex-->
            <xsd:element name="name" type="xsd:string"/>
            <!--以下两个类型是自定义类型-->
            <xsd:element name="age" type="ageType" />
            <xsd:element name="sex" type="sexType" />
        </xsd:sequence>
        <!--
            自定义属性:
                属性名:number
                属性类型:numberType
                use="required"它是必须的
        -->
        <xsd:attribute name="number" type="numberType" use="required"/>
    </xsd:complexType>
    <xsd:simpleType name="sexType"><!--sexType是个简单的类型-->
        <!--
            base="xsd:string":基本格式是字符串
            enumeration:枚举(只能是male和female之一)
        -->
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="male"/>
            <xsd:enumeration value="female"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="ageType"><!--ageType是个简单的类型-->
        <!--
            base="xsd:integer"基本数据格式integer
            minInclusive value="0":最小值是0
            maxInclusive value="256":最大值是256
        -->
        <xsd:restriction base="xsd:integer">
            <xsd:minInclusive value="0"/>
            <xsd:maxInclusive value="256"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="numberType"><!--numberType是个简单的类型-->
        <!--
            base="xsd:string":基本数据类型是字符串
            pattern:表示组成格式,heima_(加四位数字 )
            -->
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="heima_\d{4}"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema> 

解析自定义student.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<!-- 
	1.填写xml文档的根元素
	2.引入xsi前缀.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	3.引入xsd文件命名空间.  xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd"
	4.为每一个xsd约束声明一个前缀,作为标识  xmlns:a="http://www.itcast.cn/xml"设置前缀为a
		不写a代表空前缀, xmlns="http://www.itcast.cn/xml"

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

		  xsi:schemaLocation="http://www.itcast.cn/xml1  student.xsd"
		  xsi:schemaLocation="http://www.itcast.cn/xml2  student.xsd"

		  xmlns:a="http://www.itcast.cn/xml1"
		  xmlns:b="http://www.itcast.cn/xml2">

		  <a:name></name>
		  <b:name></name>
 -->
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		  xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd"
		  xmlns="http://www.itcast.cn/xml">
 	<student number="heima_0001">
 		<name>tom</name>
 		<age>18</age>
 		<sex>male</sex>
 	</student>

	<student number="heima_0003">
		<name>zhangsan</name>
		<age>100</age>
		<sex>female</sex>
	</student>
	<student number="heima_0005">
		<name>lisi</name>
		<age>45</age>
		<sex>female</sex>
	</student>
 </students>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值