​前言​

​schema约束的定义​

​将dtd文件引入到xml文档中​




前言

在这里我们介绍了xml之tdt约束,但是这种约束没法约束属性的值,比如age属性我想限制只能是数字,那就不行了,所以我们这里介绍schema约束



schema约束的定义



这里schema约束有点复杂,所以我们就直接贴代码,代码有注释



myStudent.xsd约束文件 

xml之schema约束_schema约束



<?xml version="1.0"?>
<xsd:schema xmlns="www.lingaolu.com/student/xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="www.lingaolu.com/student/xml" elementFormDefault="qualified"> <!-- www.lingaolu.com/student/xml是命名空间,可被其他文件引用的名字 -->
<xsd:element name="Student" type="StudentType" /> <!-- 定义Student标签,类型为StudentType -->
<xsd:complexType name="StudentType"> <!-- StudentType类型下定义说明 -->
<xsd:sequence>
<xsd:element name="name" type="xsd:string" minOccurs="1" maxOccurs="2"/> <!-- 定义name标签,类型为字符串 -->
</xsd:sequence>
</xsd:complexType>
</xsd:schema>



myTeacher.xsd约束文件

xml之schema约束_命名空间_02



<?xml version="1.0"?>
<xsd:schema xmlns="www.lingaolu.com/teacher/xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="www.lingaolu.com/teacher/xml" elementFormDefault="qualified"> <!-- www.lingaolu.com/teacher/xml是命名空间,可被其他文件引用的名字 -->
<xsd:element name="Teacher" type="TeacherType"/> <!-- 定义Teacher标签,类型为TeacherType -->
<xsd:complexType name="TeacherType"> <!-- TeacherType类型下定义说明 -->
<xsd:sequence>
<xsd:element name="name" type="xsd:string" minOccurs="1" maxOccurs="2"/> <!-- 定义name标签,类型为字符串 -->
</xsd:sequence>
</xsd:complexType>
</xsd:schema>


myProgrammer.xsd约束文件

这个约束文件比较复杂,引用了myStudent.xsd约束文件 

xml之schema约束_字符串_03



<?xml version="1.0"?>   <!-- www.lingaolu.com/programmer/xml是命名空间,可被其他文件引用的名字 -->
<xsd:schema xmlns="www.lingaolu.com/programmer/xml"
xmlns:student="www.lingaolu.com/student/xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="www.lingaolu.com/programmer/xml" elementFormDefault="qualified"> <!-- xmlns:student引用www.lingaolu.com/student/xml命名空间,也就是引用myStudent.xsd约束文件-->
<xsd:import namespace="www.lingaolu.com/student/xml" schemaLocation="myStudent.xsd"/> <!-- import myStudent.xsd,这个例子没用到-->
<xsd:element name="Programmers" type="ProgrammersType"/> <!-- 定义Programmers标签,类型为ProgrammersType -->
<xsd:complexType name="ProgrammersType"> <!-- ProgrammersType类型下定义说明 -->
<xsd:sequence> <!-- 定义Programmer标签,类型为ProgrammerType,最少为0,最多没有限制 -->
<xsd:element name="Programmer" type="ProgrammerType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ProgrammerType"> <!-- ProgrammerType类型下定义标签 -->
<xsd:sequence maxOccurs="unbounded">
<!--<xsd:element name="stu" type="student:StudentType" minOccurs="1" maxOccurs="1"><!&ndash; 定义stu标签,类型为student:StudentType,这里,最多一个 &ndash;>
</xsd:element>-->
<xsd:element name="spouse" type="xsd:element" minOccurs="0" maxOccurs="1"> <!-- 定义spouse配偶标签,类型为element,最多一个 -->
</xsd:element>
<xsd:element name="name" type="xsd:string"/> <!-- 定义name标签,类型为字符串 -->
<xsd:element name="age" type="ageType" /> <!-- 定义age标签,类型为ageType -->
<xsd:element name="code" type="codeType" /> <!-- 定义code标签,类型为codeType -->
</xsd:sequence>
<xsd:attribute name="id" type="idType" use="required"/> <!-- 定义id属性,类型为idType -->
</xsd:complexType>
<xsd:simpleType name="ageType"> <!-- ageType类型下定义说明 -->
<xsd:restriction base="xsd:integer"> <!-- 类型为integer -->
<xsd:minInclusive value="0"/> <!-- 最小值为0 -->
<xsd:maxInclusive value="256"/> <!-- 最小值为256 -->
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="codeType"> <!-- codeType类型下定义说明 -->
<xsd:restriction base="xsd:string"> <!-- 类型为字符串 -->
<xsd:enumeration value="java"/> <!-- 值只能为java或者python -->
<xsd:enumeration value="python"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="idType"> <!-- idType类型下定义说明 -->
<xsd:restriction base="xsd:string"> <!-- 类型为字符串 -->
<xsd:pattern value="lin_\d{4}"/> <!-- 规则为lin_+4个数字 -->
</xsd:restriction>
</xsd:simpleType>

</xsd:schema>


将dtd文件引入到xml文档中


  • 填写xml文档的根元素
  • 引入xsi前缀:xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  • 引入xsd文件命名空间
  • 为每一个xsd约束声明一个前缀,作为标识



xml之schema约束_命名空间_04



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

<Programmers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.lingaolu.com/programmer/xml ../schema/myProgrammer.xsd
http://www.lingaolu.com/student/xml ../schema/myStudent.xsd
http://www.lingaolu.com/teacher/xml ../schema/myTeacher.xsd"
xmlns="www.lingaolu.com/programmer/xml"
xmlns:s="www.lingaolu.com/student/xml"
xmlns:t="www.lingaolu.com/teacher/xml"
>
<!-- 引用3个文件 -->
<!-- xsi:schemaLocation="
http://www.lingaolu.com/programmer/xml ../schema/myProgrammer.xsd
http://www.lingaolu.com/student/xml ../schema/myStudent.xsd
http://www.lingaolu.com/teacher/xml ../schema/myTeacher.xsd" -->
<!-- 上面引用了3个xsd文件,并且为其取别名,其中第一个xmlns="www.lingaolu.com/programmer/xml"没有取别名,默认 -->
<!-- xmlns="www.lingaolu.com/programmer/xml"
xmlns:s="www.lingaolu.com/student/xml"
xmlns:t="www.lingaolu.com/teacher/xml" -->
<Programmer id="lin_0001">
<spouse>
<t:Teacher>
<t:name>陪你度过难关</t:name>
</t:Teacher>
</spouse>
<name>马对钱无趣</name>
<age>57</age>
<code>java</code>
</Programmer>
<Programmer id="lin_1001">
<spouse>
<s:Student>
<s:name>奶茶好喝</s:name>
<s:name>某某也好喝</s:name>
</s:Student>
</spouse>
<name>刘不知妻美</name>
<age>54</age>
<code>python</code>
</Programmer>

</Programmers>


引用约束后的xml文件,会遵从约束文件的约束,标签以及内容就不能随便写了



把xml文件拖到浏览器

xml之schema约束_xml_05