xml 元素名称的大小写约定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1074447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:35:47  来源:igfitidea点击:

Case conventions on element names?

xmlcase-sensitive

提问by Ian Boyd

Are there any formalrecommendations on element casing in XML?

是否有任何关于 XML 元素大小写的正式建议?

I know XHTML uses lowercase element names (as opposed to HTML which canonically uses uppercase but is case-insensitive.)

我知道 XHTML 使用小写元素名称(而不是 HTML,后者规范地使用大写但不区分大小写。)

But I'm talking about XML for generic content.

但我说的是通用内容的 XML。

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.

注意:我正在寻找引用的指南而不是意见。但是获得最多票数的意见可以被视为指导方针。

采纳答案by Pete Kirkham

Most XML standards originating from the W3C tend to use lower case with hyphens.

大多数源自 W3C 的 XML 标准倾向于使用带连字符的小写字母。

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.

将 XML 视为平台中立文档的格式(W3C 标准试图鼓励这种格式)与将 XML 视为特定于平台的对象图的序列化的 XAML 等语言之间存在哲学上的区别。

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.

如果您不使用 XML 作为平台中立的文档格式,而是作为特定于应用程序的序列化,那么您不妨省去一些麻烦,并在 XML 名称和平台特定名称之间建立 1:1 的对应关系。但是几乎任何其他对象图格式都比 XML 更好。

If you are, then you might want to fit in with XHTML, XSLT, SVG, XProc, RelaxNG and the rest.

如果是,那么您可能想要适应 XHTML、XSLT、SVG、XProc、RelaxNG 和其他。

回答by Metro Smurf

Not that it matters, but I've always been partial to PascalCase for Elements and camelCase for attributes:

这并不重要,但我一直偏爱元素的 PascalCase 和属性的驼峰:

<Root>
  <ParentElement attributeId="1">
    <ChildElement attributeName="foo" />
  </ParentElement>
</Root>

回答by lavinio

There is no formal recommendation.

没有正式的推荐。

Since XML was designed with the twin purposes of holding documentsand exchanging information between disparate systems, it was designed so as to be able to matchthe applications using it.

由于 XML 的设计具有在不同系统之间保存文档交换信息的双重目的,因此它被设计为能够匹配使用它的应用程序。

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).

所以.Net XML 倾向于使用ProperCasing(见证XAML),而其他XML 将使用camelCasing、python_conventions、dot.naming,甚至COBOL-CONVENTIONS。W3C 似乎非常喜欢小写带破折号(例如 XSLT)或 justlowercasewordssmashedtogether(例如 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. :)

我喜欢所有的小写字母和没有下划线,因为这意味着 [Shift] 键的使用较少,而且我的手指有点懒惰。:)

回答by Koert van Kleef

To add to Metro Smurf's answer.

添加到 Metro Smurf 的答案中。

The National Information Exchange Model (NIEM: http://en.wikipedia.org/wiki/National_Information_Exchange_Model) says to use:

国家信息交换模型(NIEM:http: //en.wikipedia.org/wiki/National_Information_Exchange_Model)说使用:

  • Upper CamelCase (PascalCase) for elements.
  • (lower) camelCase for attributes.
  • 大驼峰式 (PascalCase) 用于元素。
  • (下)camelCase 属性。

The NIEM makes for a good option when you're looking to conform to some standard.

当您希望符合某些标准时,NIEM 是一个不错的选择。

回答by oluies

See the UN/CEFACT XML Naming and Design Rules Technical Specification Version 3.0page 23 for some example rules used in several standards.

有关 多个标准中使用的一些示例规则,请参阅UN/CEFACT XML 命名和设计规则技术规范版本 3.0第 23 页。

Specifics (from page 23 of Version 3.0 dated 17 December 2009):

细节(来自 2009 年 12 月 17 日版本 3.0 的第 23 页):

  • LowerCamelCase (LCC) MUST be used for naming attributes.
  • UpperCamelCase (UCC) MUST be used for naming elements and types.
  • Element, attribute and type names MUST be in singular form unless the concept itself is plural.
  • 必须使用小驼峰式 (LCC) 命名属性。
  • UpperCamelCase (UCC) 必须用于命名元素和类型。
  • 元素、属性和类型名称必须是单数形式,除非概念本身是复数形式。

(other link, Swedish site)

其他链接,瑞典网站

回答by kencordero

Google's style guiderecommends (perhaps even mandates) camelCase for all element names, as well as attribute names.

Google 的样式指南建议(甚至强制要求)所有元素名称以及属性名称都使用驼峰命名法。

回答by Richard Kennard

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).

扩展我上面评论:在 XSLT 中使用“带连字符的小写字母”存在一些问题。具体来说,很容易将名为“year-from-age”的节点与公式“year-age”(例如从年份中减去年龄)混淆。

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.

正如@KarlKieninger 指出的那样,这只是人类层面的问题,而不是 XSLT 解析器的问题。但是,由于这通常不会产生错误,因此使用“带连字符的小写字母”作为标准是自找麻烦,恕我直言。

Some pertinent examples:

一些相关的例子:

<a>1</a><b>1</b>
<xsl:value-of select="a+b"/>
outputs 2, as expected

<a>1</a><b>1</b>
<xsl:value-of select="a-b"/>
DOES NOT ERROR, BUT OUTPUTS NOTHING AT ALL

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.

在上面的代码中,减法运算符之前必须至少放置一个空格,但加法运算符没有这样的要求。

<a-b>1</a-b><c>1</c>
<xsl:value-of select="a-b -c"/>
outputs 0, as expected

But note how confusing the above is to read!

但请注意,阅读以上内容是多么令人困惑!

<a>1</a><a-b>3</a-b><b>2</b>
<xsl:value-of select="a-b"/>
outputs 3

<a>1</a><a-b>3</a-b><b>2</b>
<xsl:value-of select="a -b"/>
outputs -1

The presence of a single space changes the output above, but neither variant is an error.

单个空格的存在会改变上面的输出,但两种变体都不是错误。

回答by Adam Luter

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.

XML 大小写的最初意图是带连字符的小写。它区分大小写,不需要您遵循该约定——因此您可以做任何您想做的事情。我没有引文,抱歉。

回答by DisgruntledGoat

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.

我不会说 HTML“规范地”使用大写。我认为最初使用大写字母可以更轻松地将 HTML 与内容分开。现在使用语法高亮显示,这是没有必要的。

I veer towards lowercase, with dashes if necessary (quicker to type, too). Mixing case in XML just feels wrong to me.

我转向小写,必要时使用破折号(输入也更快)。在 XML 中混合大小写对我来说是错误的。

回答by AnthonyWJones

Camel case gets my vote.

骆驼案得到了我的投票。

As to cited examples perhaps this question can be come the link people cite.

至于引用的例子,这个问题可能是人们引用的链接。