默认声明了哪些 XML 个名称空间?

Which XML namespaces are declared by default?

哪些 XML 命名空间是默认声明的,不需要 not/must 声明?

简而言之:

  • http://www.w3.org/XML/1998/namespace 前缀为 xml
  • http://www.w3.org/2000/xmlns/ 前缀为 xmlns

Namespaces in XML 1.0 (Third Edition) and the Namespaces in XML 1.1 (Second Edition) 都只定义了两个命名空间:

Namespace constraint: Reserved Prefixes and Namespace Names

The prefix xml is by definition bound to the namespace name http://www.w3.org/XML/1998/namespace. It MAY, but need not, be declared, and MUST NOT be undeclared or bound to any other namespace name. Other prefixes MUST NOT be bound to this namespace name, and it MUST NOT be declared as the default namespace.

The prefix xmlns is used only to declare namespace bindings and is by definition bound to the namespace name http://www.w3.org/2000/xmlns/. It MUST NOT be declared or undeclared. Other prefixes MUST NOT be bound to this namespace name, and it MUST NOT be declared as the default namespace. Element names MUST NOT have the prefix xmlns.

All other prefixes beginning with the three-letter sequence x, m, l, in any case combination, are reserved. This means that:

  • users SHOULD NOT use them except as defined by later specifications
  • processors MUST NOT treat them as fatal errors.

因此,只有 http://www.w3.org/XML/1998/namespace(前缀为 xml,包含少量属性,例如 xml:langsee full list here)和 http://www.w3.org/2000/xmlns/(带有前缀xmlns,用于声明其他命名空间)实际上是在标准中定义的。

某些 XML 处理库可能会将 http://www.w3.org/2001/XMLSchema(前缀为 xs)和 http://www.w3.org/2001/XMLSchema-instance(前缀为 xsi)提升到接近默认状态,并且甚至在编写 XML 文件时默认为这些名称空间写 xmlns 声明,但这只是约定,不能作为标准行为来依赖。