XML 中是否可以有多个命名空间前缀?

Is it possible to have multiple namespace prefixes in XML?

我想做这样的事情:

<root:secondlevel:thirdlevel
    xmlns:secondlevel="http://secondlevel.com"
    xmlns:secondlevel:thirdlevel="http://thirdlevel.com">
</root:secondlevel:thirdlevel>

有没有办法让这些多级别 root:secondlevel:thirdlevel 有效 XML?

不可以,XML中最多只能有一个命名空间前缀。

XML Namespace BNF rules for names are based on QName,只允许单个PrefixedName

QName          ::= PrefixedName | UnprefixedName
PrefixedName   ::= Prefix ':' LocalPart
UnprefixedName ::= LocalPart
Prefix         ::= NCName
LocalPart      ::= NCName
NCName         ::= Name - (Char* ':' Char*) /* An XML Name, minus the ":" */

PrefixLocalPart 都不允许冒号 (:) 字符,因此最多可以有一个冒号(并且最多有一个 Prefix)部分QName.

Side note: 在基本级别 XML:

语法上允许使用多个冒号
STag          ::= '<' Name (S Attribute)* S? '>'
NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
NameChar      ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
Name          ::= NameStartChar (NameChar)*

但是 W3C XML Recommendation is clear 除了命名空间之外,不应使用冒号:

Note:

The Namespaces in XML Recommendation [XML Names] assigns a meaning to names containing colon characters. Therefore, authors should not use the colon in XML names except for namespace purposes, but XML processors must accept the colon as a name character.

并且命名空间不允许多个命名空间前缀,如上所示。

另请参阅: