Schema校验XML

<schema>元素是每一个XMLSchema的根元素。

<schema>元素

<schema>元素是每一个XMLSchema的根元素:

<?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema>

<schema>元素可包含属性一个schema声明往往看上去类似这样:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
-- xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">
...
...
</xs:schema>

代码解释:

下面的片断:

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

显示用于构造schema的元素和数据类型来自"http://www.w3.org/2001/XMLSchema"命名空间。同时它还规定了来自命名空间"http://www.w3.org/2001/XMLSchema"的元素和数据类型应该使用前缀xs


这个片断:

targetNamespace="http://www.w3school.com.cn

显示本schema定义的元素和数据类型属于“http://www.w3school.com.cn”命名空间


这个片断:

xmlns="http://www.w3school.com.cn"

指出认的命名空间是"http://www.w3school.com.cn"

  • 为什么要使用命名空间(避免元素名冲突)
  • 什么是命名空间
  • 使用URL作为XML的NameSpaces
  • 命名空间的语法定义:xmlns:[prefix]="URL"
  • 元素和属性都可以应用命名空间

这个片断:

elementFormDefault="qualified"

指出任何XML实例文档所使用的且在此schema中声明过的元素必须被命名空间限定。

XML文档中引用Schema

XML文档含有对XMLSchema的引用:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
	xmlns:context="http://www.springframework.org/schema/context"  
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	 
</beans>

代码解释:

下面的片断:

http://www.springframework.org/schema/beans

规定了认命名空间的声明。此声明会告知schema验证器,在此XML文档中使用的所有元素都被声明于"http://www.springframework.org/schema/beans"这个命名空间。


一旦您拥有了可用的XMLSchema实例命名空间:

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


您就可以使用schemaLocation属性了。此属性有两个值。第一个值是需要使用的命名空间。第二个值是供命名空间使用的XMLschema的位置:

xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"























相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念