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?
Is there a way to do those multiple levels root:secondlevel:thirdlevel
as valid XML?
推荐答案
不,XML中最多只能有一个名称空间前缀。
XML命名空间名称的BNF规则基于 QName ,只允许一个 PrefixedName
:
The XML Namespace BNF rules for names are based on QName, which allows only a single PrefixedName
:
QName ::= PrefixedName | UnprefixedName
PrefixedName ::= Prefix ':' LocalPart
UnprefixedName ::= LocalPart
Prefix ::= NCName
LocalPart ::= NCName
NCName ::= Name - (Char* ':' Char*) /* An XML Name, minus the ":" */
既不是前缀
也不是 LocalPart
允许冒号(:
)字符,因此最多只有一个冒号(最多一个前缀
)部分到 QName
。
Neither Prefix
nor LocalPart
allow colon (:
) characters, so there can be at most one colon (and at most one Prefix
) part to a QName
.
旁注:在基本级别XML语法上允许多个冒号:
Side note: multiple colons are syntactically allowed in base level 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建议很清楚除名称空间外不应使用冒号:
But the W3C XML Recommendation is clear that colons should not be used except for namespaces purposes:
注意:
XML建议书中的命名空间 [XML Names] 为包含冒号字符的名称指定
含义。因此,作者
不应使用XML名称中的冒号,除了命名空间用途,
但XML处理器必须接受冒号作为名称字符。
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.
并且命名空间不允许多个名称空间前缀,如上所示。
And Namespaces do not allow multiple namespace prefixes as shown above.
参见:
- What does the XML syntax with a colon mean?
- Is a colon a legal first character in an XML tag name?
这篇关于XML中是否可以有多个名称空间前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!