美文网首页
简单知识

简单知识

作者: ifeelok0319 | 来源:发表于2017-07-05 16:25 被阅读8次

在学习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>

相关文章

  • 简单知识

    1.var关键词的作用域是最近的函数作用域(如果在函数体的外部就是全局作用域), let 关键词的作用域是最接近的...

  • 简单知识

    在学习spring的时候需要配置一个xml文件。大概是这样的。 以前没怎么学习过xml文件,学过之后做个笔记。 参...

  • mongoDB简单知识

    Windows 平台安装 MongoDB 下载链接 一步一步next即可。 将mongo加入系统服务,在shell...

  • 简单冷知识

    1 自然地打断别人说话 2 初次见面显得更加友好 3 如何判断谁喜欢你 4 怎么判断别人是不是在盯着你看 或者 5...

  • 知识蒸馏-简单

    参考文献: https://github.com/DA-southampton/NLP_ability/blob/...

  • 勤学

    姬滢灏 勤学,说来简单,说来难。简单于知识学会就好,难于想学会知识并没有那么简单...

  • 2021-2022曲靖市九年级上册期末试卷

    简单,超级简单!知识点也超多!

  • 线程知识 和 RunLoop简单知识

    我的博客: 博客链接 上面有关于 线程 和 RunLoop的知识,请大家多多指点。

  • 写给小学生及家长的眼健康常识

    简单明了的眼知识:

  • 2019-04-24

    JavaScript简单知识点总结

网友评论

      本文标题:简单知识

      本文链接:https://www.haomeiwen.com/subject/wnuqhxtx.html