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"的命名空间下的,它的作用是把命名空间下的schema文件给引用进来,让ide能解析和验证xml文件是否符合语法规范.

对于Spring框架的XML配置,可以使用XSDXML Schema Definition)文件来定义和验证配置文件的结构和内容。XSD文件定义了配置文件中可以使用的元素、属性和它们之间的关系。 在Spring框架中,可以使用以下步骤来配置XSD: 1. 首先,确保你已经包含了Spring的命名空间定义和相关的Schema Location。例如,你可以在XML文件的顶部添加以下命名空间声明: ```xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" ``` 还需要提供Schema Location,指定XSD文件的位置,例如: ```xml xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" ``` 2. 然后,在配置文件的根元素中使用Spring的命名空间和相应的元素。例如,要配置Spring的上下文,可以使用`<context:component-scan>`元素: ```xml <context:component-scan base-package="com.example" /> ``` 3. 最后,根据具体的需求,在配置文件中使用其他Spring元素和属性进行配置。根据XSD文件的定义,这些元素和属性应该符合预先定义的规则。 通过这些步骤,你可以使用XSD文件来配置Spring框架的XML文件,确保配置的正确性和一致性。请注意,具体的XSD文件和命名空间可能会因不同的Spring版本而有所不同,所以请根据你使用的Spring版本来选择合适的XSD文件和命名空间。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值