schema约束快速入门
PS:schema约束是一种约束xml文件的方法,其具体实现是通过创建xsd文件编写相关程序完成的。
schema约束
- schema符合xml的语法,xml语句。一个xml中可以有多个chema,多个schema使用名称空间区分(类似于java包名), dtd里面有PCDATA类型,但是在schema里面可以支持更多的数据类型,比如 年龄 只能是整数,在schema可以直接定义一个整数类型, schema语法更加复杂,schema目前不能替代dtd。
创建xsd文件
- 因为xsd文件和xml的规范性相同,故其文件头也为
<?xml version="1.0" encoding="UTF-8"?>
- xsd文件的根节点为
<schema></schema>
- 根节点的属性
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.baidu.com/20220408"
elementFormDefault="qualified">
</schema>
- xmlns表示当前xml文件是一个约束文件
- targetNamespace表示使用schema约束文件,直接通过这个地址引入约束文件,这个地址任意写,但要保证和xml文件中的xmlns中的地址一样
- elementFormDefault="qualified"是一个默认选项
xsd的复杂元素
<element name = "person">
<complexType>
<sequence>
<element name = "name" type = "string" maxOccurs="unbounded"></element>
<element name = "age" type = "int"></element>
<element name = "adress" type="string"></element>
</sequence>
<attribute name = "id" type = "int" use = "required"></attribute>
</complexType>
</element>
-
在schema约束当中必须用标准语言规范,如果xml中是复杂元素,需要引用
<complexType></complexType>
,该标签的子标签可以是<squence></squence>
表示xml文件中标签表示顺序必须按照squence下的顺序严格执行,也可以是<choice></choice>
表示只出现choice下任意一个标签,还可以是<any></any>
表示出现任意元素,<all></all>
表示元素只能出现一次。 -
标签元素中属性
maxOccurs="unbounded"
表示元素的出现的次数 -
约束xml根标签的属性时,需要用到
<attribute name="id1" type="int" use="required"></attribute>
,其中name是属性名称,type是属性类型,use表示属性是否必须出现。
xml引入schema约束文件
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.baidu.com/20220408"
xsi:schemaLocation="http://www.baidu.com/20220408 schema_test.xsd" id = "001">
<name>ss</name>
<age>20</age>
<adress>ssss</adress>
</person>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
表示xml是一个被约束文件xmlns="http://www.baidu.com/20220408 schema"
是约束文档里面 targetNamespacexsi:schemaLocation="http://www.baidu.com/20220408 schema_test.xsd">
targetNamespace 空格 约束文档的地址路径
复杂的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="string"></element>
</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="employee1">
<complexType>
<sequence>
<!-- 引用任何一个元素 -->
<any></any>
<element name="name" type="string"></element>
<element name="age" type="int"></element>
<element name="sex" type="string"></element>
</sequence>
<attribute name="number" type="string"></attribute>
</complexType>
</element>
<element name="employee2">
<complexType>
<sequence>
<any></any>
<element name="name" type="string"></element>
<element name="age" type="int"></element>
<element name="sex" type="string"></element>
</sequence>
<attribute name="number" type="string"></attribute>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
company.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 数据文件 引用多个Schema -->
<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"
>
<employee1 number="3300">
<!-- 部门名称 -->
<dept:name>HR</dept:name>
<!-- 员工名称 -->
<name>王晓晓</name>
<age>23</age>
<sex>nv</sex>
</employee1>
<employee2 number="3200">
<dept:name>PD</dept:name>
<name>王老二</name>
<age>45</age>
<sex>nan</sex>
</employee2>
</company>
根元素的属性xmlns:dept(:xsi)此处的dept和xsi只是一个别名,没有具体含义,只是方便引用。当引入多个schema约束文件时,使用 别名:名称 可以避免重复。