分为:
DTD约束
Schema约束:
1.是基于DTD的替代者
2.描述XML文档的结构
使用:
1.创建 XML schema File 文件
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.hello.org/Product"
xmlns:tns="http://www.hello.org/Product"
elementFormDefault="qualified">
<element name="product">
<complexType>
<sequence>
<element name="id" type="int"/>
<element name="name" type="string"/>
<element name="price" type="float"/>
</sequence>
</complexType>
</element>
</schema>
通常:xmlns="http://www.w3.org/2001/XMLSchema"-----标准,不改动
targetNamespace="http://www.hello.org/Product------命名空间--对应约束的标签(“公司网址/创建schema文件的名称”)
xmlns:tns="http://www.hello.org/Product" ----------默认命名空间
2.引用约束
必须按照建立的约束的顺序、类型、个数进行引用
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引用schame约束文件 -->
<product xmlns="http://www.hello.org/Product"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hello.org/Product Product.xsd">
<id>123</id>
<name>af</name>
<price>3.3</price>
</product>
xmlns="http://www.hello.org/Product" --------指定命名空间
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"--------标准
xsi:schemaLocation="http://www.hello.org/Product Product.xsd"> ----------第一个参数为命名空间、第二个参数为文件名称