简单知识

在学习spring的时候需要配置一个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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="****" />

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <bean id="velocityConfigurer"
        class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath">
            <value>/WEB-INF/vm/</value>
        </property>
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">utf-8</prop>
                <prop key="output.encoding">utf-8</prop>
            </props>
        </property>
    </bean>

</beans>

以前没怎么学习过xml文件,学过之后做个笔记。

参考文献:

  1. XML之命名空间的作用(xmlns)
  2. 关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation

命名空间解决的问题

XML的元素名字是不固定的,当两个不同的文档使用同样的名称描述两个不同类型的元素的时候,或者一个同样的标记表示两个不同含义的内容的时候,就会发生命名冲突。

命名空间(Namespace),对于每一套特定应用的DTD,给它一个独一无二的标志来代表,如果在XML中使用DTD中定义的元素,需将DTD的标志和元素名,属性连在一起使用,相当于指明了元素来自什么地方,这样就不会同其他同名元素混淆了。在XML中,采用现成的,在全球范围唯一的域名作为Namespace,即URL作为XML的Namespace

命名空间的语法

xmlns:[prefix]="url"

其中xmlns:是必须的属性。prefix是命名空间的别名,它的值不能为xml。如果没有别名,则为默认默认命名空间。

  1. 默认Namespace xmlns="url"
  2. 指定了父元素的命名空间,子元素希望用自己的命名空间,可以在子元素中指定命名空间的别名。
  3. 属性也可以有自己的命名空间。

这些命名空间都有一些规范,包括含有什么元素、属性等,这些规范写在命名空间为http://www.w3.org/2001/XMLSchema-instance(即xsi)的schemaLocation(xsi:"schemaLocation")元素中。

xsi:schemaLocation

XML Schema提供了两个在实例文档中使用的特殊属性,用于指出模式文档的位置。这两个属性是:xsi:schemaLocationxsi:noNamespaceSchemaLocation,前者用于声明了目标名称空间的模式文档,后者用于没有目标名称空间的模式文档,它们通常在实例文档中使用。

它定义了XML Namespace和对应的 XSD(Xml Schema Definition)文档的位置的关系。它的值由一个或多个URI引用对组成,两个URI之间以空白符分隔(空格和换行均可)。第一个URI是定义的 XML Namespace的值,第二个URI给出Schema文档的位置,Schema处理器将从这个位置读取Schema文档,该文档的targetNamespace必须与第一个URI相匹配。

xsd文件示例:

<!--?xml version="1.0" encoding="UTF-8"?-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns="http://www.sunxin.org/book" 
           targetnamespace="http://www.sunxin.org/book" 
           elementformdefault="qualified">
    <xs:element name="book" type="bookType"></xs:element>
    <xs:complextype name="bookType">
        <xs:sequence>
            <xs:element name="title" type="xs:string"></xs:element>
            <xs:element name="author" type="xs:string"></xs:element>
        </xs:sequence>
    </xs:complextype>
</xs:schema>

对应xml文件示例:

<!--?xml version="1.0" encoding="GB2312"?-->
<book xmlns="http://www.sunxin.org/book" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://www.sunxin.org/book http://www.sunxin.org/ 
book.xsd">
    <title>《Struts 2深入详解》</title>
    <author>孙鑫</author>
</book>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,087评论 19 139
  • 1. XML简介 以下内容来自于http://www.w3school.com.cn/xml 基本知识 XML 和...
    WebSSO阅读 1,974评论 1 7
  • 最近继续看Struts,在看struts.xml配置的时候看到这个: [html]view plaincopy p...
    ad8d261a83e4阅读 1,071评论 0 1
  • 经过两天的奋战,终于把xml简略的过了一遍。 1.1XML介绍 xml是Extensible Markup lan...
    Ystrator阅读 871评论 0 2
  • Schema与DTD XML Schema符合XML语法结构。 DOM、SAX等XML API很容易解析出XML ...
    扒块腹肌阅读 2,170评论 0 4