XML SCHEMA

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

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

### 如何使用 XML Schema 定义和验证 XML 文档结构 #### 创建 XML Schema 文件 为了定义 XML 文档的结构,需编写一个 XML Schema (XSD) 文件。此文件描述允许哪些元素及其属性存在于目标 XML 中,并规定它们的数据类型。 ```xml <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- 定义根元素 --> <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> ``` 每个 XSD 文件都应以 `<xs:schema>` 作为根节点[^3]。 #### 验证 XML 文档 一旦有了 XSD 文件,则可以通过编程方式或者利用专门工具来检验给定的 XML 是否遵循该模式: - **Java 实现** 对于 Java 应用程序而言,可以借助 JAXP API 来加载并解析 XSD 和待校验XML 文件,从而执行验证操作[^2]。 ```java import javax.xml.XMLConstants; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.SchemaFactory; public class XmlValidator { public static void main(String[] args) throws Exception { String language = XMLConstants.W3C_XML_SCHEMA_NS_URI; SchemaFactory factory = SchemaFactory.newInstance(language); // 加载schema StreamSource schemaFile = new StreamSource(new File("path/to/schema.xsd")); var schema = factory.newSchema(schemaFile); // 使用schema对象来进行验证... } } ``` 除了上述方法外,在某些情况下也可以直接在 Web 浏览器中打开带有 DTD 或者 XSD 的 XML 文件查看其有效性;不过更常见的是采用命令行工具或是集成开发环境中的插件完成这项工作。 XML Schema 不仅提供了比传统 DTD 更丰富的功能集——比如更好的数据类型支持、命名空间兼容性和更强的表现力等特性,还具备良好的可读性与维护便利度[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值