XML 元素是否有标准的命名约定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/442529/
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
Is there a standard naming convention for XML elements?
提问by tpower
Is there any standard, de facto or otherwise, for XML documents? For example which is the "best" way to write a tag?
XML 文档是否有任何事实上的或其他的标准?例如,哪个是编写标签的“最佳”方式?
<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"/>
回答by Marc Gravell
I suspect the most common values would be camelCased - i.e.
我怀疑最常见的值是驼峰式的 - 即
<myTag someAttribute="someValue"/>
In particular, the spaces cause a few glitches if mixed with code-generators (i.e. to [de]serialize xml to objects), since not many languages allow enums with spaces (demanding a mapping between the two).
特别是,如果与代码生成器混合使用空格会导致一些小故障(即 [反] 将 xml 序列化为对象),因为没有多少语言允许带有空格的枚举(需要两者之间的映射)。
回答by Farhad Maleki
XML Naming Rules
XML 命名规则
XML elements must follow these naming rules:
XML 元素必须遵循以下命名规则:
- Element names are case-sensitive
- Element names must start with a letter or underscore
- Element names cannot start with the letters xml(or XML, or Xml, etc)
- Element names can contain letters, digits, hyphens, underscores, and periods
- Element names cannot contain spaces
Any name can be used, no words are reserved (except xml).
可以使用任何名称,不保留任何字词(xml 除外)。
Best Naming Practices
最佳命名实践
- Create descriptive names, like this: <person>, <firstname>, <lastname>.
- Create short and simple names, like this: <book_title> not like this: <the_title_of_the_book>.
- Avoid "-". If you name something "first-name", some software may think you want to subtract "name" from "first".
- Avoid ".". If you name something "first.name", some software may think that "name" is a property of the object "first".
- Avoid ":". Colons are reserved for namespaces (more later).
- Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your software doesn't support them.
Naming Styles
命名风格
There are no naming styles defined for XML elements. But here are some commonly used:
没有为 XML 元素定义命名样式。但这里有一些常用的:
- Lower case <firstname> All letters lower case
- Upper case <FIRSTNAME> All letters upper case
- Underscore <first_name> Underscore separates words
- Pascal case <FirstName> Uppercase first letter in each word
- Camel case <firstName> Uppercase first letter in each word except the first
回答by PhiLho
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...).
我只是注意到很多 XML 方言只使用小写名称(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:
我没有得到“属性值中没有空格”规则。不知何故,它引发了“什么放在属性中,什么放在文本中?”的争论。
也许这些不是最好的例子,但有一些众所周知的 XML 格式在属性中使用空格:
- 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...
- XHTML,特别是 class 属性(您可以放置两个或多个类),当然还有 alt 和 title 属性。
- SVG,例如路径标签的 d 属性。
- 两者都带有样式属性...
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. :-)
哦,你不需要在自动关闭斜线之前有一个空格。:-)
回答by Raithlin
I favour TitleCase for element names, and camelCase for attributes. No spaces for either.
我喜欢 TitleCase 元素名称,camelCase 属性。两者都没有空格。
<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.
顺便说一句,我快速搜索了 XML 中的最佳实践,并找到了这个相当有趣的链接:XML 模式:最佳实践。
回答by annakata
I would tend to favour lowercaseor camelcasetags and since attributes should typically reflect data values - not content - I would stick to a value which could be used as a variable name in whatever platform/language might be interested, i.e. avoid spacesbut the other two forms couldbe ok
我倾向于偏爱小写或驼峰标记,因为属性通常应该反映数据值 - 而不是内容 - 我会坚持一个值,该值可以在任何可能感兴趣的平台/语言中用作变量名,即避免空格但另一个两种形式都可以
回答by alistair
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>)而不是不使用分隔符来增强可读性。参考:http: //www.w3schools.com/xml/xml_elements.asp。所以根据 w3schools 的答案是:
<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.
该值不需要使用下划线或分隔符,因为属性值中允许有空格,但元素标记名称中不允许有空格。
回答by Pete Kirkham
Many document centred XML dialects use lower case basic Latin and dash. I tend to go with that.
许多以文档为中心的 XML 方言使用小写的基本拉丁语和破折号。我倾向于这样做。
Code generators which maps XML directly to programming language identifiers are brittle, and (with the exception of naive object serialisation, such as XAML) should be avoided in portable document formats; for best reuse and information longevity the XML should try to match the domain, not the implementation.
将 XML 直接映射到编程语言标识符的代码生成器是脆弱的,并且(除了朴素的对象序列化,如 XAML)应该避免使用可移植文档格式;为了获得最佳重用和信息寿命,XML 应该尝试匹配域,而不是实现。
回答by evermeire
rss is probably one of the most consumed xml schemas in the world and it is camelCased.
rss 可能是世界上使用最多的 xml 模式之一,它是驼峰式的。
Spec is here: http://cyber.law.harvard.edu/rss/rss.html
规范在这里: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
lastBuildDate 管理编辑器 pubDate
回答by micksatana
I normally align XML naming convention with the same naming convention in other parts of code. The reason is when I load the XML into Object its attributes and element names can be referred as the same naming convention currently used in the project.
我通常在代码的其他部分将 XML 命名约定与相同的命名约定对齐。原因是当我将 XML 加载到 Object 时,它的属性和元素名称可以引用为当前在项目中使用的相同命名约定。
For example, if your javascript using camelCase then your XML uses camelCase as well.
例如,如果您的 javascript 使用驼峰命名,那么您的 XML 也使用驼峰命名。
回答by Jeson Martajaya
Microsoft embraces two convention:
Microsoft 包含两个约定:
For configuration, Microsoft uses camelCase. Look at Visual Studio config file. For VS2013, it is stored in:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe.config
对于配置,Microsoft 使用camelCase。查看 Visual Studio 配置文件。对于VS2013,它存储在:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe.config
Example:
例子:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
- Microsoft also uses UpperCasefor their XAML. I guess it is to differentiate from HTML (which uses lowercase).
- Microsoft 还使用大写字母作为他们的 XAML。我想这是为了区别于 HTML(使用小写字母)。
Example:
例子:
<MenuItem Header="Open..." Command="ApplicationCommands.Open">
<MenuItem.Icon>
<Image Source="/Images/folder-horizontal-open.png" />
</MenuItem.Icon>
</MenuItem>

