元素名称的大小写约定?
对于 XML 中的元素大小写是否有任何正式建议?
我知道 XHTML 使用小写元素名称(与规范使用大写但不区分大小写的 HTML 不同)。
但我谈论的是通用内容的 XML。
小写:
<customer>
<accountnumber>619</accountnumber>
<name>Shelby Lake</name>
</customer>
camelCase:
<customer>
<accountNumber>619</accountNumber>
<name>Shelby Lake</name>
</customer>
PascalCase:
<Customer>
<AccountNumber>619</AccountNumber>
<Name>Shelby Lake</Name>
</Customer>
大写:
<CUSTOMER>
<ACCOUNTNUMBER>619</ACCOUNTNUMBER>
<NAME>Shelby Lake</NAME>
</CUSTOMER>
注意:我正在寻找引用的指南而不是意见。 但得票最多的意见可以被视为指导方针。
Are there any formal recommendations on element casing in XML?
I know XHTML uses lowercase element names (as opposed to HTML which canonically uses uppercase but is case-insensitive.)
But I'm talking about XML for generic content.
lowercase:
<customer>
<accountnumber>619</accountnumber>
<name>Shelby Lake</name>
</customer>
camelCase:
<customer>
<accountNumber>619</accountNumber>
<name>Shelby Lake</name>
</customer>
PascalCase:
<Customer>
<AccountNumber>619</AccountNumber>
<Name>Shelby Lake</Name>
</Customer>
UPPERCASE:
<CUSTOMER>
<ACCOUNTNUMBER>619</ACCOUNTNUMBER>
<NAME>Shelby Lake</NAME>
</CUSTOMER>
Note: I'm looking for cited guidelines rather than opinions. But the opinion with the most up-votes can be considered a guideline.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
大多数源自 W3C 的 XML 标准倾向于使用带有连字符的小写字母。
将 XML 视为平台中立文档的格式(W3C 标准试图鼓励这种格式)与将 XML 视为平台特定对象图的序列化(例如 XAML)的语言之间存在哲学上的区别。
如果您不使用 XML 作为平台中立的文档格式,而是作为特定于应用程序的序列化,那么您不妨省去一些麻烦,并在 XML 名称和平台特定名称之间建立 1:1 的对应关系。 但就这一目的而言,几乎任何其他对象图格式都比 XML 更好。
如果您尝试以平台中立的方式使用它,那么您可能希望适应 XHTML、XSLT、SVG、XProc、RelaxNG 等。
关于在标识符和 XSLT 表达式中使用连字符 - 在许多编程语言中添加空格会改变代码的含义(Python、JavaScript),并且值得注意的编程语言允许在标识符中使用连字符,例如 LISP。 因此,这是否困扰您更多的是您使用这些语言的经验,而不是 XML 命名本身。
Most XML standards originating from the W3C tend to use lower case with hyphens.
There is a philosophical distinction between seeing XML as a format for platform neutral documents, which W3C standards try to encourage, and languages such as XAML which see XML as a serialisation of a platform specific object graph.
If you're not using XML as a platform neutral document format, but as an application specific serialisation, then you might as well save yourself some bother and have a 1:1 correspondence between the XML names and the platform specific names. But almost any other object graph format is better than XML for that purpose.
If you are trying to use it in a platform neutral way, then you might want to fit in with XHTML, XSLT, SVG, XProc, RelaxNG and the rest.
On the use of hyphens in identifiers and XSLT expressions - there are many programming languages where adding whitespace will change the meaning of code (Python, JavaScript), and notable programming languages which allow hyphens in identifiers, such as LISP. So whether this bothers you is more about your experience with those languages than about XML naming itself.
这并不重要,但我一直偏爱元素的 PascalCase 和属性的 CamelCase:
Not that it matters, but I've always been partial to PascalCase for Elements and camelCase for attributes:
没有正式的建议。
由于 XML 的设计具有保存文档和在不同系统之间交换信息的双重目的,因此它被设计为能够匹配使用它的应用程序。
因此.Net XML倾向于使用ProperCasing(XAML就是如此),而其他XML将使用camelCasing、python_conventions、dot.naming,甚至COBOL-CONVENTIONS。 W3C 似乎相当喜欢小写加破折号(例如 XSLT)或只是小写单词混合在一起(例如 MathML)。
我喜欢全部小写,没有下划线,因为这意味着更少使用 [Shift] 键,而且我的手指有点懒。 :)
There is no formal recommendation.
Since XML was designed with the twin purposes of holding documents and exchanging information between disparate systems, it was designed so as to be able to match the applications using it.
So .Net XML tends to use ProperCasing (witness XAML), while other XML will use camelCasing, python_conventions, dot.naming, and even COBOL-CONVENTIONS. The W3C seems to like lower-case-with-dashes-quite-a-bit (e.g. XSLT) or justlowercasewordssmashedtogether (e.g. MathML).
I like all lower case and no underscores, since that means less use of the [Shift] key, and my fingers are a little lazy. :)
添加到地铁蓝精灵的答案。
国家信息交换模型(NIEM:http://en.wikipedia.org/wiki/National_Information_Exchange_Model)表示使用:
当您希望符合某些标准时,NIEM 是一个不错的选择。
To add to Metro Smurf's answer.
The National Information Exchange Model (NIEM: http://en.wikipedia.org/wiki/National_Information_Exchange_Model) says to use:
The NIEM makes for a good option when you're looking to conform to some standard.
扩展我上面的评论:在 XSLT 中使用“带连字符的小写字母”存在一些问题。 具体来说,很容易将名为“year-from-age”的节点与公式“year -age”(例如,从年份中减去年龄)混淆。
正如 @KarlKieninger 指出的,这只是人类层面的问题,而不是 XSLT 解析器的问题。 然而,由于这通常不会产生错误,因此使用“带连字符的小写字母”作为标准是自找麻烦,恕我直言。
一些相关示例:
在上面的代码中,减法运算符之前必须至少放置一个空格,但加法运算符则没有这样的要求。
但请注意,上面的内容读起来多么令人困惑!
单个空格的存在会改变上面的输出,但这两种变体都不是错误。
To expand on my comment above: the use of 'lowercase with hyphens' has some problems in XSLT. Specifically it is easy to confuse a node called, say, 'year-from-age' with a formula 'year - age' (e.g. subtract age from year).
As @KarlKieninger points out, this is only a problem at the human level and not for the XSLT parser. However since this will often not produce an error, using 'lowercase with hyphens' as a standard is asking for trouble, IMHO.
Some pertinent examples:
In the above code, you must put at least one space before a subtraction operator, but there is no such requirement for an addition operator.
But note how confusing the above is to read!
The presence of a single space changes the output above, but neither variant is an error.
请参阅 UN/CEFACT XML 命名和设计规则技术规范版本 3.0 第 23 页提供了多个标准中使用的一些示例规则。
详细信息(来自 2009 年 12 月 17 日版本 3.0 的第 23 页):
(其他链接,瑞典语网站)
See the UN/CEFACT XML Naming and Design Rules Technical Specification Version 3.0 page 23 for some example rules used in several standards.
Specifics (from page 23 of Version 3.0 dated 17 December 2009):
(other link, Swedish site)
Google 风格指南 建议(甚至可能强制)所有元素名称使用驼峰式命名法,以及属性名称:
Google's style guide recommends (perhaps even mandates) camelCase for all element names, as well as attribute names:
XML 大小写的最初目的是带有连字符的小写字母。 它区分大小写,并且不要求您遵循该约定——因此您可以做任何您想做的事情。 我没有引用,抱歉。
The original intent for XML casing was lower case with hyphens. It's case sensitive and doesn't require you follow that convention -- so you can do whatever you want. I have no citations, sorry.
我不会说 HTML “规范地”使用大写字母。 我认为最初使用大写字母是为了在视觉上更容易地将 HTML 与内容分开。 如今有了语法高亮,这就没有必要了。
我倾向于小写,必要时使用破折号(打字也更快)。 我觉得在 XML 中混合大小写是不对的。
I wouldn't say HTML "canonically" uses uppercase. I think originally uppercase was used to visually separate HTML from the content more easily. With syntax highlighting nowadays, that's just not necessary.
I veer towards lowercase, with dashes if necessary (quicker to type, too). Mixing case in XML just feels wrong to me.
骆驼案得到了我的投票。
至于引用的例子,也许这个问题可以来自人们引用的链接。
Camel case gets my vote.
As to cited examples perhaps this question can be come the link people cite.