什么是XML文件中的'xmlns'和'xsi:schemaLocation'?
出版日期:
更新:
技术文章 > 什么是XML文件中的'xmlns'和'xsi:schemaLocation'?
2026 状态和安全使用
结论: xmlns 将前缀或默认命名空间绑定到 URI,因此元素名称保持明确。 xsi:schemaLocation 只是将命名空间 URI 与架构位置配对的提示;验证行为仍然取决于 XML 处理器。
你将学到什么
- 默认命名空间和前缀命名空间如何更改元素的扩展名称。
- 为什么命名空间 URI 是一个标识符并且不必返回网页。
xsi:schemaLocation与xsi:noNamespaceSchemaLocation有何不同。
适用人群:阅读 Maven、Spring、SOAP 或其他 XML 配置文件的开发人员。
2026 年立场:W3C 命名空间和 XML 架构规则仍然具有权威。将远程架构位置视为不受信任的输入,并避免在生产解析期间依赖网络检索。
安全注意事项:原始文章中的命令和配置示例尚未在当前生产环境中重新运行。应用之前,请在单独的测试环境中验证支持的版本、备份、访问控制和回滚步骤。
官方主要来源
概述
本文说明 XML 文件中 “xmlns” 和 “xsi:schemaLocation” 的含义。
目录
- 什么是XML文件?
- 1-1. 什么是 "xmlns "和 "xsi:schemaLocation"?
- 1-2. 什么是xmlns?
- 1-3. 什么是 "xmlns:xsi"?
- 摘要
1. 什么是XML文件?
XML是可扩展标记语言的缩写,由万维网联盟(W3C)指定。 除了 "xmlns "和 "xsi:schemaLocation "之外的更详细的规范,请参考以下网站。
1-1. 什么是 "xmlns "和 "xsi:schemaLocation"?
下面的例子是使用Spring框架进行Java应用开发时,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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
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/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
xmlns 用于声明 XML 命名空间,xsi:schemaLocation 用于给出命名空间与模式文档位置的对应关系。 在 Spring 配置中,命名空间用于区分不同词汇表中的元素;相应的 XML Schema 则描述允许出现的元素、属性和结构。
1-2. 什么是xmlns?
xmlns 是 XML 命名空间声明。命名空间名称通常使用 URI,以便在不同 XML 词汇表之间提供全局唯一的标识。
在 “xmlns="http://www.springframework.org/schema/beans"” 中,该 URI 声明了默认命名空间;没有前缀的元素名都属于这个命名空间。
命名空间 URI 是标识符,不要求能够作为网页打开。解析器或框架可以通过 xsi:schemaLocation、内置目录或类路径中的资源找到相应的 XSD。Spring 通常会从其 JAR 中解析这些模式,而不是每次都通过网络下载。
1-3. 什么是 "xmlns:xsi"?
“xmlns:xsi” 把前缀 xsi 绑定到 XML Schema Instance 命名空间。随后可以使用 xsi:schemaLocation 等带前缀的属性。没有前缀的 “xmlns” 声明默认命名空间;“xmlns:context” 之类的声明则要求使用 context:component-scan 这样的限定名称。
2. 摘要
xmlns 负责标识元素或属性所属的命名空间,xsi:schemaLocation 则向处理器提供模式位置提示。两者用途不同,不能混为同一个“定义文件设置”。