- 创建一个schema文件, 其后缀名为.xsd
**其根节点
**在schema文件里面
(属性 xmlns=“http://www.w3.org/2001/XMLSchema” -表示当前xml文件是一个约束文件
targetNamespace=“http://www.example.org/one”
-使用schema约束文件,直接通过这个地址引入约束文件
elementFormDefault=“qualified”
-质量良好
步骤:
(1)看xml中有多少个元素
< element>
(2) 看简单元素和复杂元素
< completeType>
< sequence>
子元素
< /sequense>
< /completeType>
(3)简单元素写在 复杂元素的里面
<element name="person">
<complexType>
<sequence>
<element name="name" type="string"></element>
<element name="age" type="int"></element>
</sequence>
</complexType>
</element>
(4)在xml(被约束文件)中 添加约束文件
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:(xsi)/*取个别名*/="http://www.w3.org/2001/XMLSchema-instance" /*表示xml是一个被约束文件*/
xmlns="http://www.example.org/one"/*是约束文档里的 yargetNamespace*/
xsi:schemaLocation="http://www.example.org/one one.xsd">/*targetNamespace +空格 约束文档的地址路径(因为在同一个目录下所以可以不添加路径*/
在根节点里面加入
XMLSchema复杂元素指示器
All:表示只能出现一次
Choice:只能出现其中的一个
Sequence:元素按照顺序出现
maxOccurs="unbounded"表示出现次数没有限制,例:<element name = "unbounded" type="double" maxOccurs="unbounded"/>
定义属性(必须是复杂元素):<attribute name = "p1" type="string" use="required"></attribute>
<any></any>:表示任意元素
一个xml文件中可以引入多个schema文件,但是只能引入一个dtd文件