XML和XSD

初识XML和XSD

最近在学习Spring,由于要写大量的xml配置文件,而且配置文件中也用到了xsd的相关知识,于是整理一些本人不熟悉的知识点.建议先学习html的相关知识.本文部分内容参考w3school.

XML

  1. 类似HTML树结构,具有自我描述性语法,语法也类似html,不作多说.
  2. xml命名空间:如果两个不同的文档使用相同的元素名时,就会发生命名冲突.
file1.xml
<root>
    <name>A</name>
</root>

file2.xml
<root>
    <age>12</age>
</root>

如果上面两个xml文档被一起使用,由于两个文档都包含不同内容和定义的<root>元素,就会发生命名冲突.
解决方法:

// 使用前缀来避免命名冲突
file1.xml
<a:root>
    <name>A</name>
</a:root>

file2.xml
<b:root>
    <age>12</age>
</b:root>
// 使用命名空间
file1.xml
<a:root xmlns:a="url1">
    <a:name>A</a:name>
</a:root>

file2.xml
<b:root xmlns:b="url2">
    <b:age>12</b:age>
</b:root>

与仅仅使用前缀不同,为root标签添加了一个xmlns属性,为前缀赋予了一个与命名空间相关联的限定名称.
3.XML的xmlns(xml namespace)属性:xml命名空间属性被放置于元素的开始标签之中,使用以下语法:xmlns:namespace-prefix="namespaceURI".注意:命名空间中的url不会被解析器用于查找信息,唯一的作用就是赋予命名空间的一个唯一的名称,但是许多公司会作为指针来使用命名空间指向的实际存在的网页,这个网页包含关于命名空间的信息.
4.xml默认的命名空间:为元素定义默认的命名空间可以让我们省去在所有子元素中使用前缀的工作xmlns="namespaceURL"

<root xmlns="URL">
    <name>haha</name>
</root>

XSD

  1. 什么是XML schema?XML Schema 的作用是定义 XML 文档的合法构建模块,类似 DTD.它定义了可出现在文档的元素,属性,元素的次序和数目,数据类型,默认值固定值,哪些元素是子元素等等,根据我的理解,通俗来说就是定义xml文档的规范,虽然在普通的xml文档中,有什么元素都行,只要符合xml语法.但是引入了xsd就不行了,你必须按照定义来,它对元素有严格的规定,什么元素不该出现,就不该出现,还有一系列你自己定义的其他规则.
  2. <schema>元素是每一个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)xmlns:xs="http://www.w3.org/2001/XMLSchema":表明schema中用到的元素和数据类型出自此命名空间,同时规定来自此命名空间的元素和数据都要使用前序xs.
(2)targetNamespace="http://www.w3school.com.cn":显示 schema 中用到的元素和数据类型来自此命名空间.
(3)elementFormDefault="qualified":指出任何xml实例文档所使用的且在此schema中声明过的元素必须被命名空间限定.
3. 在xml文档中引用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":指定了默认的命名空间声明,告知schema 验证器,该xml中使用的所有元素都被声明于此命名空间中
(2)xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance":拥有了xml schema的实例命名空间
(3)xsi:schemaLocation="http://www.w3school.com.cn note.xsd":使用schemaLocation属性,该属性第一个值是需要使用的命名空间,第二个值为供命名空间使用的schema的位置.

结合Spring配置文件

  1. 尽管对只学习了xml和xsd表面的内容,但是应该对以下经典的Spring的配置文件不再陌生了:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd "
       default-autowire="byName">
</beans>

个人理解(可能不准确或者有错):
①首先xmlns部分都是命名空间,表明该xml元素来自这些命名空间;比如你就不能无缘无故打出<test>haha</test>,因为默认的命名空间xmlns="http://www.springframework.org/schema/beans"根本没有这个元素,其中一些带有前缀的如tx,aop表示如果要使用该命名空间下的元素必须要使用该前缀指明,例如<context:component-scan base-package="com.test"/>,说明component-scan元素是在"http://www.springframework.org/schema/context"该命名空间下的.
②后面的schemaLocation元素是"http://www.w3.org/2001/XMLSchema-instance"的命名空间下的,它的作用是把命名空间下的文件给引用进来,让ide能解析和验证xml文件是否符合语法规范.

猜你喜欢

转载自blog.csdn.net/qq_37993487/article/details/80275612