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

最关键的就是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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值