什么是XML(extensible mark language)
顾名思义,可扩展标记语言。用来描述信息,不同于编程语言,编程语言在于逻辑的表达,也就是思维的体现。xml主要是用来描述不同信息,比如,配置文件,webservice用来传递的数据。
什么是XSD(xml schema definition)
顾名思义,xml schema的定义,也就是用来指定某个xml文件的数据的约束信息,进行大的层面的对文件的约束。其本身也是xml文件,故也收到xml文件的约束。
实例讲解_XML
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd">
<!-- 开启代理模式 -->
<aop:aspectj-autoproxy/>
<!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 -->
<context:component-scan base-package="com.ctbu">
<!-- base-package 如果多个,用“,”分隔 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 自动装配 -->
<context:annotation-config />
</beans>
上述文件就是一个典型的spring的配置的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
,xml文件必须,指定当前文件的编码和版本号。xmlns="http://www.springframework.org/schema/beans"
属性
为xml namespace,即整个beans标签中默认namespace为此属性。xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
此属性为所有标签的满足xsd文件约束,具体约束文件地址,则有xsi:schemaLocation属性来描述。xmlns:aop="http://www.springframework.org/schema/aop"
aop前缀的命名空间。
*xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
对命名空间对应xsi(xml schema instance)的指定,格式为命名空间 相应的xsd文件
实例讲解_XSD
<xsd:schema xmlns="http://www.springframework.org/schema/aop" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tool="http://www.springframework.org/schema/tool" targetNamespace="http://www.springframework.org/schema/aop" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:element name="config">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
A section (compartmentalization) of AOP-specific configuration (including
aspects, pointcuts, etc).
]]>
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="pointcut" type="pointcutType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
A named pointcut definition.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="advisor" type="advisorType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation source="java:org.springframework.aop.Advisor">
<![CDATA[
A named advisor definition.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="aspect" type="aspectType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
A named aspect definition.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="proxy-target-class" type="xsd:boolean" default="false">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
Are class-based (CGLIB) proxies to be created? By default, standard
Java interface-based proxies are created.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="expose-proxy" type="xsd:boolean" default="false">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
Indicate that the proxy should be exposed by the AOP framework as a
ThreadLocal for retrieval via the AopContext class. Off by default,
i.e. no guarantees that AopContext access will work.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
前缀xsd的命名空间。targetNamespace="http://www.springframework.org/schema/aop"
作用于哪个对应的命名空间
对于xsd文件而言,本身就是对可以进行import的包含,有些类似于jsp的include标签,都是为了将文件进行模块化。那么,xsd文件就是对targetNamespace中相对应的xml进行约束,也就是描述每个element的规范文档。
如何利用XSD文件对XML进行校验
平时,我们都在IDE环境下开发,在书写框架配置文件的时候,也是到处copy和有IDE提示。有时候,就会想IDE怎么知道当前xml配置文件中应该包含哪些元素?而且怎么会有不符合规范的错误信息提示?
原因猜想:IDE会根据xsi:schemaLocation属性中寻找相应的xsd文件,并加载到本地,通过对xsd文件进行解析,我们在进行相应提示快捷键的事件之后,那么,IDE就会将转换了xsd文件的数据结构,转换成我们相应的xml中提示信息。当我们写入不规范xml元素过后,IDE会进行xsd文件中描述信息进行校验,并且给出错误提示。