Is there a standard naming convention for XML elem

2019-01-10 16:09发布

Is there any standard, de facto or otherwise, for XML documents? For example which is the "best" way to write a tag?

<MyTag />
<myTag />
<mytag />
<my-tag />
<my_tag />

Likewise if I have an enumerated value for an attribute which is better

<myTag attribute="value one"/>
<myTag attribute="ValueOne"/>
<myTag attribute="value-one"/>

13条回答
三岁会撩人
2楼-- · 2019-01-10 16:38

For me, it is like discussing of code style for a programming language: some will argue for a style, others will defend an alternative. The only consensus I saw is: "Choose one style and be consistent"!

I just note that lot of XML dialects just use lowercase names (SVG, Ant, XHTML...).

I don't get the "no spaces in attributes values" rule. Somehow, it sends to the debate "what to put in attributes and what to put as text?".
Maybe these are not the best examples, but there are some well known XML formats using spaces in attributes:

  • XHTML, particularly class attribute (you can put two or more classes) and of course alt and title attributes.
  • SVG, with for example the d attribute of the path tag.
  • Both with style attribute...

I don't fully understand the arguments against the practice (seem to apply to some usages only) but it is legal at least, and quite widely used. With drawbacks, apparently.

Oh, and you don't need a space before the auto-closing slash. :-)

查看更多
你好瞎i
3楼-- · 2019-01-10 16:39

rss is probably one of the most consumed xml schemas in the world and it is camelCased.

Spec is here: http://cyber.law.harvard.edu/rss/rss.html

Granted it has no node attributes in the schema, but all the node element names are camelCased. For example:

lastBuildDate managingEditor pubDate

查看更多
Explosion°爆炸
4楼-- · 2019-01-10 16:40

There is no explicit recommendation. Based on other recommendation from W3C, the one for XHTML, I've opted for lowercase:

4.2. Element and attribute names must be in lower case

XHTML documents must use lower case for all HTML element and attribute names. This difference is necessary because XML is case-sensitive e.g. <li> and <LI> are different tags.

查看更多
三岁会撩人
5楼-- · 2019-01-10 16:42

I favour TitleCase for element names, and camelCase for attributes. No spaces for either.

<AnElement anAttribute="Some Value"/>

As an aside, I did a quick search for Best Practices in XML, and came up with this rather interesting link: XML schemas: Best Practices.

查看更多
Luminary・发光体
6楼-- · 2019-01-10 16:42

XML Naming Rules

XML elements must follow these naming rules:

  • Names can contain letters, numbers, and other characters
  • Names cannot start with a number or punctuation character
  • Names cannot start with the letters xml (or XML, or Xml, etc)
  • Names cannot contain spaces Any name can be used, no words are reserved.

Source: W3 School

查看更多
Deceive 欺骗
7楼-- · 2019-01-10 16:44

It's subjective, but if there are two words in an element tag, the readibility can be enhanced by adding an underscore between words (e.g. <my_tag>) instead of using no separator. Reference: http://www.w3schools.com/xml/xml_elements.asp. So according to w3schools the answer would be:

<my_tag attribute="some value">

The value needn't use an underscore or separator, since you are allowed spaces in attribute values but not in element tag names.

查看更多
登录 后发表回答