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"。
这个片断: 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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |