这两天在用WSAD用spring做一个项目,项目中spring的配置xml文件总是显示红色,但是运行一切正常。看看xsd一切正常,自动完成功能也很正常。没办法先把xml 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>
[1] xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
这个就是指明了schema的namespace显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs
也就是说以后你定义的xsd 用到的数据类型等来自这个命名空间。
[2] targetNamespace="http://www.w3school.com.cn"
显示被此 schema 定义的元素 (note, to, from, heading, body) 来自命名空间: "http://www.w3school.com.cn"
也就是所在xsd中用到的元素来自这个命名空间。
[3]xmlns="http://www.w3school.com.cn"
本身这个schema的命名空间
下面来看一下对schema的引用:
<?xml version="1.0"?> <note xmlns="http://www.w3school.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3school.com.cn note.xsd"> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
[1]xmlns="http://www.w3school.com.cn"
这个是这个xml文件的命名空间
[2]xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
定义了一个xml schema的实例 对应前面schema的命名空间的定义,就像java中new了一个对象。
[3]xsi:schemaLocation="http://www.w3school.com.cn note.xsd"
第一个值是需要使用的命名空间。第二个值是供命名空间使用的 XML schema 的位置:
下面再来对照一下spring的定义:
首先看schema的定义,下面这个片段来自: http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
<xsd:schema xmlns="http://www.springframework.org/schema/tx" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:tool="http://www.springframework.org/schema/tool" targetNamespace="http://www.springframework.org/schema/tx" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:import namespace="http://www.springframework.org/schema/beans" /> <xsd:import namespace="http://www.springframework.org/schema/tool" /> </xsd:schema>
注意这个文档的属性定义的顺序和前面的sample是不一样的。这里首先定义了这个schema本身的命名空间
[1]xsd:schema xmlns=http://www.springframework.org/schema/tx
定义了这个shcema的namespace
[2]xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
定义了这个schema用到的元素的命名空间。
[3]targetNamespace="http://www.springframework.org/schema/tx"
这个schema的默认适用对象。
有了上面的这个schema就可以定义一个能够被它验证的xml文件了:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-lazy-init="true"> <!-- the DataSource (parameterized for configuration via a PropertyPlaceHolderConfigurer) --> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${DB2Driver}"/> <property name="url" value="${URL}"/> <property name="username" value="${UserName}"/> <property name="password" value="${PassWord}"/> </bean> <!-- the transactional advice (i.e. what 'happens'; see the <aop:advisor/> bean below) --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- the transactional semantics... --> <tx:attributes> <!-- all methods starting with 'get' are read-only --> <!-- tx:method name="get*" read-only="true"/ --> <!-- other methods use the default transaction settings (see below) --> <tx:method name="*"/> </tx:attributes> </tx:advice> </beans>
[1] xmlns="http://www.springframework.org/schema/beans"
默认的命名空间 比如<bean> tag默认用到的命名空间
[2]xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
一个schema的实例
[3]xmlns:tx=http://www.springframework.org/schema/tx
定义了另外一个命名空间 tx ,如 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 就是引用这个命名空间的
[4] xsi:schemaLocation 就是指定了用到的验证schema的地址,如tx用到的就是前面定义的那个schema