XML入门:schema开发过程

Schema的快速入门:

  • 创建一个Schema文件,后缀名是.xsd   根节点: <Schema></Schema>

---------

Schema中

-----------属性:xmlns="http://www.w3.org/2001/XMLSchema"  表示当前xml文件是一个约束文件

-----------属性:targetNamespace="http://www.example.org/1"  表示使用Schema约束文件,可以通过这个地址来引入

-----------属性:elementFormDefault="qualified"

步骤:(1) 看xml中有多少个元素  <element>

(2)看是简单元素还是复杂元素

-------复杂元素:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/1" xmlns:tns="http://www.example.org/1" elementFormDefault="qualified">
      <element name="person">
         <complexType>
           <sequence>
                 子元素
           </sequence>
         </complexType>
      </element>
</schema>

 

-------简单元素:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/1" xmlns:tns="http://www.example.org/1" elementFormDefault="qualified">
      <element name="person">
         <complexType>
           <sequence>
             <element name="name" type="string"></element>
             <element name="age" type="int"></element>
           </sequence>
         </complexType>
      </element>
</schema>

(4) 在xml中引入约束文件

person.xml

<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.example.org/1" xsi:schemaLocation="http://www.example.org/1 1.xsd">
   <name>zhangsan</name>
   <age>18</age>
</person>
  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 表示xml是被约束的文件
  • xmlns="http://www.example.org/1" 是约束文档里面的 argetNamespace
  • xsi:schemaLocation="http://www.example.org/1 1.xsd" argetNamespace(+空格+)约束文档地址

1.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/1" xmlns:tns="http://www.example.org/1" elementFormDefault="qualified">
      <element name="person">
         <complexType>
           <sequence>
             <element name="name" type="string"></element>
             <element name="age" type="int"></element>
           </sequence>
         </complexType>
      </element>
</schema>

Schema

 

阅读剩余
THE END