小说网站 搜小说 无限网 烟雨红尘 小说爱好者 免费小说 免费小说网站

根据Schema写出XML文档四部曲

Schema约束文档本身就是一个XML文档,扩展名为xsd

难点:XML文档的根元素怎么写?

如下4步曲:

a、首先看Schema文档,找到根元素

<?xml version="1.0"encoding="UTF-8"?>

<书架></书架>

b、思考:书架来自于哪个名称空间?看Schema文档,targetNamespace就是名称空间。

用xmlns关键字(xmlns名称空间声明)来声明我的元素来自哪个名称空间(xmlns:xml namespace)

 

<?xml version="1.0"encoding="UTF-8"?>

<itcast:书架 xmlns:itcast="http://www.itcast.cn"></itcast:书架>

 

c、思考:名称空间与哪个xsd文件对应呢?使用schemaLocation关键字来关联名称空间和xsd的对应关系

 

<?xml version="1.0"encoding="UTF-8"?>

<itcast:书架 xmlns:itcast="http://www.itcast.cn"

                     schemaLocation="http://www.itcast.cnbook.xsd"></itcast:书架>


剩下的任务就交给eclipse自动生成就行了。

案例如下:

已知Schema约束文档如下,写出对应的xml文档

<?xml version="1.0" encoding="UTF-8" ?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
					  targetNamespace="http://www.itcast.cn"
					  elementFormDefault="qualified">
	<xs:element name='书架' >
		<xs:complexType>
			<xs:sequence maxOccurs='unbounded' >
				<xs:element name='书' >
					<xs:complexType>
						<xs:sequence>
							<xs:element name='书名' type='xs:string' />
							<xs:element name='作者' type='xs:string' />
							<xs:element name='售价' type='xs:integer' />
						</xs:sequence>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

xml文档如下:

<?xml version="1.0" encoding="UTF-8"?>
<itcast:书架 xmlns:itcast="http://www.itcast.cn"
				xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
				xsi:schemaLocation="http://www.itcast.cn book.xsd">
	<itcast:书>
		<itcast:书名>JavaScript网页开发</itcast:书名>
		<itcast:作者>张孝祥</itcast:作者>
		<itcast:售价>28</itcast:售价>
	</itcast:书>

</itcast:书架>

最终版本:以秒杀的方式建立。

直接通过Myeclipse工具创建即可。

posted on 2016-05-28 16:39  王小航  阅读(853)  评论(0编辑  收藏  举报

导航