在xml中如何引用自己定义的schema文件?

来源:互联网 发布:js 数组元素 函数 编辑:程序博客网 时间:2024/05/22 17:50

最关键的就是xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”这句话
意思是:自己这个文档的命名空间,可以方便其它xml或着schema文件引入。
方式一:通过命名空间引入
第一步:创建自己的01.xsd文件。如下:

<?xml version="1.0" encoding="UTF-8"?><schema xmlns="http://www.w3.org/2001/XMLSchema"     targetNamespace="http://www.example.org/01"    xmlns:tns="http://www.example.org/01"     elementFormDefault="qualified">    <element name="user">    <complexType>        <sequence>            <element name="id" type="int"/>            <element name="username" type="string"/>            <element name="born" type="date"/>        </sequence>        </complexType>    </element></schema>

第二步:编写01.xml,在01.xml中可以引入自己定义的01.xsd文件,如下:

<?xml version="1.0" encoding="UTF-8"?><user  xmlns="http://www.example.org/01"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://www.example.org/01">     <id>1</id>     <username>zhangsan</username>     <born>1989-12-20 </born></user>

第二种方式:通过文件路径引入:
示例如下:

<?xml version="1.0" encoding="UTF-8"?><user  xmlns="http://www.example.org/01"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:noNamespaceSchemaLocation="01.xsd">     <id>1</id>     <username>zhangsan</username>     <born>1999-12-23</born></user>
0 0
原创粉丝点击