XML SCHEMA

本文深入解析Spring框架中XML Schema的基本概念及其应用。从Schema的基本结构出发,详细解释了如何通过Schema定义XML文件的结构和命名空间,以及如何利用Schema进行XML文件的有效验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    这两天在用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值