Case conventions on element names?

给你一囗甜甜゛ 提交于 2019-12-17 10:34:06

问题


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.


回答1:


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, then you might want to fit in with XHTML, XSLT, SVG, XProc, RelaxNG and the rest.




回答2:


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

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



回答3:


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




回答4:


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:

  • Upper CamelCase (PascalCase) for elements.
  • (lower) camelCase for attributes.

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




回答5:


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

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

(other link, Swedish site)




回答6:


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:

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




回答7:


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




回答8:


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.




回答9:


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.




回答10:


Camel case gets my vote.

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



来源:https://stackoverflow.com/questions/1074447/case-conventions-on-element-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!