冒号是XML标记名称中合法的第一个字符吗? [英] Is a colon a legal first character in an XML tag name?

查看:163
本文介绍了冒号是XML标记名称中合法的第一个字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 W3C XML建议 ,起始标记名具有以下定义:

According to the W3C XML Recommendation, start tag-names have the definition:

STag ::= '<' Name (S Attribute)* S? '>'

..其中Name是:

Name ::= NameStartChar (NameChar)*
NameStartChar ::= ":" | [A-Z] | ...

..(n.b.指出冒号可以作为第一个字符出现),表明以下内容是有效的XML文档:

..(n.b., states that a colon can appear as the first character) suggesting the following is a valid XML document:

<?xml version="1.0" ?><:doc></:doc>

..但是我尝试使用的任何解析器都将冒号显示为格式错误.

..but any parser I try this in shows the colon as a formatting error.

此外,在附录B(现在是文档的折旧部分)下,它明确声明:

Also, under Appendices B (though now a depreciated part of the document) it explicitly states:

Characters ':' and '_' are allowed as name-start characters.

.. and:

<?xml version="1.0" ?><_doc></_doc>

我尝试过的XML解析器接受

...

..is accepted by the XML parsers I've tried.

那么,冒号是标记名中的有效第一个字符,而我使用的解析器是错误的,还是我读取的规范是错误的?

So, is a colon a valid first character in a tag-name, and the parsers I'm using are wrong, or am I reading the specification wrong?

推荐答案

是的,在基本XML级别上,冒号(:)被允许作为名称开始字符.您引用的BNF规则明确指定了这一点.

Yes, at the base XML level, colon (:) is allowed as a name-start character. The BNF rules you cite clearly specify this.

但是, W3C XML建议书很明确除非出于命名空间的目的,否则不应使用冒号:

However, the W3C XML Recommendation is clear that colons should not be used except for namespaces purposes:

注意:

XML建议中的命名空间 [XML名称] 分配一个 含义是包含冒号的名称.因此,作者 除非出于命名空间的目的,否则不应在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.

XML命名空间标签的BNF规则基于 QName ,该名称只允许在冒号中用冒号分隔,而LocalPart:

And the XML Namespace BNF rules for tags are based on QName, which allow for colon in a name only as a separated between Prefix and LocalPart:

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

可能会问为什么从一开始就不允许在NameStartChar中使用冒号.如果幸运的话,C.M. Sperberg-McQueen可能会提供权威的解释.但是,我怀疑这是关于如何设计名称空间的不断发展的观念的问题.

One might ask why colon wasn't disallowed in NameStartChar from the beginning. If we're lucky, C. M. Sperberg-McQueen may offer an authoritative explanation. However, I suspect it's a matter of an evolving notion of how namespaces were expected to be designed.

W3C XML建议书的 1996年首次发布的工作草案 STag ,其中

The first published working draft in 1996 of the W3C XML Recommendation had a definition of STag which did not allow colon:

STag  ::= '<' Name (S Attribute)* S? '>'
Name  ::= (Letter | '-') (Letter | Digit | '-' | '.')*

到1998年, Name

By 1998, colons were allowed in Name,

Name  ::= (Letter | '_' | ':') (NameChar)*

早期格式有关结肠使用的警告,请阅读:

and an earlier form of the admonition about colon use read:

注意:XML名称中的冒号保留用于试验名称空间.预期其含义是 在将来的某个时间标准化,在那时这些文件 将冒号用于实验目的可能需要进行更新. (不能保证XML采用任何名称空间机制. 实际上将使用冒号作为名称空间分隔符.)实际上, 这意味着作者不应该在XML名称中使用冒号,除非 作为命名空间实验的一部分,但是XML处理器应该 接受冒号作为名称字符.

Note: The colon character within XML names is reserved for experimentation with name spaces. Its meaning is expected to be standardized at some future point, at which point those documents using the colon for experimental purposes may need to be updated. (There is no guarantee that any name-space mechanism adopted for XML will in fact use the colon as a name-space delimiter.) In practice, this means that authors should not use the colon in XML names except as part of name-space experiments, but that XML processors should accept the colon as a name character.

人们预料到了这种需求,但是当冒号首次被引入标签名称时,确切的形式也许还不为人所知.

The need was anticipated but the precise form perhaps was not yet known when colon was first introduced to tag names.

这篇关于冒号是XML标记名称中合法的第一个字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆