XML对Schema文档的引用123

Schema系列文章

1、XML对Schema文档的引用123

2、Schema文档概述123

3、Schema之简单元素、复合元素和属性

4、Schema约束

5、Schema指示器

6、如何创建一份XMLSchema And XML Elements

 

Schema的事例文档:

 

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="unionDemo"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/ test.xsd"
    xmlns:mstns="http://tempuri.org/test.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  
</xs:schema>

一步步分析:

 xmlns:xs="http://www.w3.org/2001/XMLSchema"

指明了在schema中使用的元素和数据种类来自http://www.w3.org/2001/XMLSchema名称空间(namespace)。它也指定了来自"http://www.w3.org/2001/XMLSchema"名称空间(namespace)的元素和数据种类必须带前缀“xs:”

 

targetNamespace="http://tempuri.org/test.xsd"

暗示了由这份schema定义的元素来自"http://tempuri.org/unionDemo.xsd"名称空间,

 

 

xmlns="http://tempuri.org/test.xsd"

指明了默认名称空间

 

 

 elementFormDefault="qualified"

指明了由这份schema声明的XML实例文档里用到的任何元素,都必须是有效的名称空间;

 

XML的事例文档:

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

 

的确是这样,新建的XML文件就是这么简单朴素,让我们来一步步健全她吧!!

首先根据相应的Schema文档写根元素,然后指定XML Schema Instance的名称空间;

 

xmlns:ns="http://www.w3.org/2001/XMLSchema-instance"

一旦你有了可以利用的XML Schema Instance的名称空间,你就可以使用schemaLcation属性了,

 

 

ns:schemaLocation ="http://tempuri.org/test.xsd test.xsd"

这个属性有两个值。第一个值是要用到的名称空间(一般认为是Schema文件里的targetNamespace的值)。第二个值是为名称空间(前一个参数)指定了需要使用的XML schema的位置。

 

 

xmlns="http://tempuri.org/test.xsd"

指定了默认的名称空间(default namespace)声明。这个声明告诉schema-检验器:这份XML文档里用到的所有元素都在http://tempuri.org/test.xsd的名称空间(namespace)中声明过(也就是Schema文件的targetNamespace的值);

经过以上几步,这份XML就算是与Schema关联上了,得到的结果:

<?xml version="1.0" encoding="utf-8" ?>
<Person xmlns:ns="http://www.w3.org/2001/XMLSchema-instance" 
 ns:schemaLocation ="http://tempuri.org/test.xsd test.xsd" 
xmlns="http://tempuri.org/test.xsd">
  
</Person>

 

 

接下来的输入你必须按照schema的描述来完成XML,否则就会报错,

更多参考http://www.cnblogs.com/caoxch/archive/2006/11/17/563856.html

posted @ 2010-12-02 08:11  麻将我会  阅读(970)  评论(0编辑  收藏  举报