5分钟搞定Schema

事先声明:搞定不是精通,只是能够运用!
其实schema很简单,之前写过DTD,比DTD学习成本还低!最近做的项目基于文件系统的,一堆的xml文件,所以想写个验证,也方便IDE操作,既然推荐Schema,那就用Schema :D
不废话,看xml文件。

<?xml version="1.0" encoding="gb2312"?>
<tests id="1" name="2">
<name>Ivan</name>
<test id="test1">
<name>hi</name>
</test>
<test id="test2">
<name>hello</name>
</test>
</tests>

这个应该算是个比较典型的xml文件了。看schema怎么写。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.iteye.com"
xmlns:tns="http://www.iteye.com"
elementFormDefault="qualified">
<!-- 一个tests标签-->
<xs:element name="tests">
<!-- 由于tests标签由属性和其它标签组成,所以是复合类型 -->
<xs:complexType>
<!-- 这是一个序列,包含所有的子标签-->
<xs:sequence>
<!--叫name的子标签-->
<xs:element name="name"/>
<!--叫test的子标签,maxOccurs是出现的次数,这里不定,能多次出现-->
<xs:element name="test" maxOccurs="unbounded">
<!--又是一个复合类型-->
<xs:complexType>
<xs:sequence>
<xs:element name="name"/>
</xs:sequence>
<!--test的id属性-->
<xs:attribute name="id"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<!--testsde id和name属性-->
<xs:attribute name="id"/>
<xs:attribute name="name"/>
</xs:complexType>
</xs:element>
</xs:schema>

很简单吧!其实就是个xml文件而已!敲一遍就知道是什么意思了!
将Schema添加到xml文件里面,如下:

<?xml version="1.0" encoding="gb2312"?>
<tests id="1" name="2" xmlns="http://www.stsoft.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.iteye.com ../test.xsd">
<name>Ivan</name>
<test id="test1">
<name>hi</name>
</test>
<test id="test2">
<name>hello</name>
</test>
</tests>

这样就可以了,../test.xsd是相对路径,如果test.xsd和xml在同一目录下,直接写名字就可以了。
应该没多少人会用到Schema,知识积累,以备后用 :D
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值