XML元素是否有标准的命名约定?

时间:2022-09-27 21:21:58

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"/>

14 个解决方案

#1


39  

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

特别是,如果与代码生成器混合(即[de]序列化xml到对象),空间会导致一些小故障,因为没有多少语言允许使用空间的枚举(需要两者之间的映射)。

#2


19  

XML Naming Rules

XML elements must follow these naming rules:

    - 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

reference http://www.w3schools.com/xml/xml_elements.asp

参考http://www.w3schools.com/xml/xml_elements.asp

#3


13  

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.
  • XHTML,特别是类属性(可以放置两个或多个类),当然还有alt和title属性。
  • SVG, with for example the d attribute of the path tag.
  • SVG,例如路径标记的d属性。
  • 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. :-)

哦,在自动关闭斜线之前你不需要空格。:-)

#4


12  

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模式:最佳实践。

#5


8  

I would tend to favour lowercase or camelcase tags 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 spaces but the other two forms could be ok

我会倾向于小写或以来camelcase标签和属性应该通常反映数据值——不满足——我将坚持一个值可以作为一个变量名在任何平台/语言可能会感兴趣,即避免空间,但其他两种形式可能是好的

#6


8  

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:

这是主观的,但是如果元素标记中有两个单词,那么可以通过在单词之间添加下划线(例如 )来增强可读性,而不用使用分隔符。参考: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.

值不需要使用下划线或分隔符,因为属性值中允许空格,但元素标记名中不允许空格。

#7


7  

Many document centred XML dialects use lower case basic Latin and dash. I tend to go with that.

许多以文档为中心的XML方言使用小写基础拉丁语和dash。我倾向于这样。

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应该尝试匹配域,而不是实现。

#8


3  

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

rss可能是世界上使用最频繁的xml模式之一,并且使用了camelcases。

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:

假定它在模式中没有节点属性,但所有节点元素的名称都用camel大小写字母表示。例如:

lastBuildDate managingEditor pubDate

lastBuildDate managingEditor发布日期

#9


2  

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加载到对象中时,它的属性和元素名称可以被引用为当前项目中使用的相同命名约定。

For example, if your javascript using camelCase then your XML uses camelCase as well.

例如,如果javascript使用camelCase,那么XML也使用camelCase。

#10


2  

Microsoft embraces two convention:

微软拥抱两个公约:

  1. For configuration, Microsoft uses camelCase. Look at Visual Studio config file. For VS2013, it is stored in:

    对于配置,微软使用camelCase。查看Visual Studio配置文件。对于VS2013,它存储在:

    C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe.config

    C:\Program Files (x86)\Microsoft Visual Studio IDE 12.0 \ Common7 \ \ devenv.exe.config

Example:

例子:

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
  1. Microsoft also uses UpperCase for their XAML. I guess it is to differentiate from HTML (which uses lowercase).
  2. 微软也使用大写的XAML。我猜它是为了区别于HTML(它使用小写)。

Example:

例子:

<MenuItem Header="Open..." Command="ApplicationCommands.Open">
    <MenuItem.Icon>
        <Image Source="/Images/folder-horizontal-open.png" />
    </MenuItem.Icon>
</MenuItem>

#11


1  

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

没有明确的建议。基于对XHTML的其他W3C推荐,我选择了小写:

4.2. Element and attribute names must be in lower case

4.2。元素和属性名必须是小写的。

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.

XHTML文档必须对所有HTML元素和属性名使用小写。这种差异是必要的,因为XML是区分大小写的,例如

  • 是不同的标记。

  • 是不同的标记。
  • #12


    0  

    XML Naming Rules

    XML的命名规则

    XML elements must follow these naming rules:

    XML元素必须遵循这些命名规则:

    • 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)
    • 名称不能以xml(或xml、或xml等)字母开头
    • Names cannot contain spaces Any name can be used, no words are reserved.
    • 名称不能包含任何可以使用的名称,也不能保留任何单词。

    Source: W3 School

    来源:W3学校

    #13


    0  

    I have been searching a lot for a good approach, also reading this thread and some others and I would vote for using hyphens.

    我一直在寻找一种好的方法,也读过这篇文章和其他一些文章,我会投票赞成使用连字符。

    They are used broadly in ARIA ( https://developer.mozilla.org/de/docs/Web/Barrierefreiheit/ARIA ) which can be seen in many source codes and are therefore common. As already pointed out here, they are certainly allowed, which is also explained here: Using - in XML element name

    它们广泛用于ARIA (https://developer.mozilla.org/de/docs/Web/Barrierefreiheit/ARIA),可以在许多源代码中看到,因此很常见。正如这里已经指出的,它们当然是允许的,这里也解释了:在XML元素名称中使用-

    Also as a side benefit: When writing HTML in combination with CSS, you often have classes whose names use hyphens as separator by default as well. Now, if you have custom tags that use CSS classes or custom attributes for tags that use CSS classes, then something like:

    另一个好处是:在与CSS结合编写HTML时,您经常会出现一些类,它们的名称在默认情况下也使用连字符作为分隔符。现在,如果您有使用CSS类的自定义标记或使用CSS类的标记的自定义属性,那么如下所示:

    <custom-tag class="some-css-class">

    <自定义标记类= " some-css-class>

    is more consistent and reads - in my humble opinion - much nicer than:

    在我的拙见下,它比:

    <customTag class="some-css-class">

    < customTag类= " some-css-class " >

    #14


    -1  

    Check this link....XML Standards and Conventions

    检查这个链接....XML标准和惯例

    EDIT: This link is broken, the new link is below: http://docs.oracle.com/cd/E82085_01/141/funtional_artifacts_guide/or-fasg-standards.htm

    编辑:此链接被破坏,新链接如下:http://docs.oracle.com/cd/E82085_01/141/funtional_artifacts_guide/or-fasg-standards.htm

    #1


    39  

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

    特别是,如果与代码生成器混合(即[de]序列化xml到对象),空间会导致一些小故障,因为没有多少语言允许使用空间的枚举(需要两者之间的映射)。

    #2


    19  

    XML Naming Rules

    XML elements must follow these naming rules:

        - 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
    

    reference http://www.w3schools.com/xml/xml_elements.asp

    参考http://www.w3schools.com/xml/xml_elements.asp

    #3


    13  

    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.
    • XHTML,特别是类属性(可以放置两个或多个类),当然还有alt和title属性。
    • SVG, with for example the d attribute of the path tag.
    • SVG,例如路径标记的d属性。
    • 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. :-)

    哦,在自动关闭斜线之前你不需要空格。:-)

    #4


    12  

    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模式:最佳实践。

    #5


    8  

    I would tend to favour lowercase or camelcase tags 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 spaces but the other two forms could be ok

    我会倾向于小写或以来camelcase标签和属性应该通常反映数据值——不满足——我将坚持一个值可以作为一个变量名在任何平台/语言可能会感兴趣,即避免空间,但其他两种形式可能是好的

    #6


    8  

    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:

    这是主观的,但是如果元素标记中有两个单词,那么可以通过在单词之间添加下划线(例如 )来增强可读性,而不用使用分隔符。参考: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.

    值不需要使用下划线或分隔符,因为属性值中允许空格,但元素标记名中不允许空格。

    #7


    7  

    Many document centred XML dialects use lower case basic Latin and dash. I tend to go with that.

    许多以文档为中心的XML方言使用小写基础拉丁语和dash。我倾向于这样。

    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应该尝试匹配域,而不是实现。

    #8


    3  

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

    rss可能是世界上使用最频繁的xml模式之一,并且使用了camelcases。

    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:

    假定它在模式中没有节点属性,但所有节点元素的名称都用camel大小写字母表示。例如:

    lastBuildDate managingEditor pubDate

    lastBuildDate managingEditor发布日期

    #9


    2  

    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加载到对象中时,它的属性和元素名称可以被引用为当前项目中使用的相同命名约定。

    For example, if your javascript using camelCase then your XML uses camelCase as well.

    例如,如果javascript使用camelCase,那么XML也使用camelCase。

    #10


    2  

    Microsoft embraces two convention:

    微软拥抱两个公约:

    1. For configuration, Microsoft uses camelCase. Look at Visual Studio config file. For VS2013, it is stored in:

      对于配置,微软使用camelCase。查看Visual Studio配置文件。对于VS2013,它存储在:

      C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe.config

      C:\Program Files (x86)\Microsoft Visual Studio IDE 12.0 \ Common7 \ \ devenv.exe.config

    Example:

    例子:

    <startup useLegacyV2RuntimeActivationPolicy="true">
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    
    1. Microsoft also uses UpperCase for their XAML. I guess it is to differentiate from HTML (which uses lowercase).
    2. 微软也使用大写的XAML。我猜它是为了区别于HTML(它使用小写)。

    Example:

    例子:

    <MenuItem Header="Open..." Command="ApplicationCommands.Open">
        <MenuItem.Icon>
            <Image Source="/Images/folder-horizontal-open.png" />
        </MenuItem.Icon>
    </MenuItem>
    

    #11


    1  

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

    没有明确的建议。基于对XHTML的其他W3C推荐,我选择了小写:

    4.2. Element and attribute names must be in lower case

    4.2。元素和属性名必须是小写的。

    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.

    XHTML文档必须对所有HTML元素和属性名使用小写。这种差异是必要的,因为XML是区分大小写的,例如

  • 是不同的标记。

  • 是不同的标记。
  • #12


    0  

    XML Naming Rules

    XML的命名规则

    XML elements must follow these naming rules:

    XML元素必须遵循这些命名规则:

    • 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)
    • 名称不能以xml(或xml、或xml等)字母开头
    • Names cannot contain spaces Any name can be used, no words are reserved.
    • 名称不能包含任何可以使用的名称,也不能保留任何单词。

    Source: W3 School

    来源:W3学校

    #13


    0  

    I have been searching a lot for a good approach, also reading this thread and some others and I would vote for using hyphens.

    我一直在寻找一种好的方法,也读过这篇文章和其他一些文章,我会投票赞成使用连字符。

    They are used broadly in ARIA ( https://developer.mozilla.org/de/docs/Web/Barrierefreiheit/ARIA ) which can be seen in many source codes and are therefore common. As already pointed out here, they are certainly allowed, which is also explained here: Using - in XML element name

    它们广泛用于ARIA (https://developer.mozilla.org/de/docs/Web/Barrierefreiheit/ARIA),可以在许多源代码中看到,因此很常见。正如这里已经指出的,它们当然是允许的,这里也解释了:在XML元素名称中使用-

    Also as a side benefit: When writing HTML in combination with CSS, you often have classes whose names use hyphens as separator by default as well. Now, if you have custom tags that use CSS classes or custom attributes for tags that use CSS classes, then something like:

    另一个好处是:在与CSS结合编写HTML时,您经常会出现一些类,它们的名称在默认情况下也使用连字符作为分隔符。现在,如果您有使用CSS类的自定义标记或使用CSS类的标记的自定义属性,那么如下所示:

    <custom-tag class="some-css-class">

    <自定义标记类= " some-css-class>

    is more consistent and reads - in my humble opinion - much nicer than:

    在我的拙见下,它比:

    <customTag class="some-css-class">

    < customTag类= " some-css-class " >

    #14


    -1  

    Check this link....XML Standards and Conventions

    检查这个链接....XML标准和惯例

    EDIT: This link is broken, the new link is below: http://docs.oracle.com/cd/E82085_01/141/funtional_artifacts_guide/or-fasg-standards.htm

    编辑:此链接被破坏,新链接如下:http://docs.oracle.com/cd/E82085_01/141/funtional_artifacts_guide/or-fasg-standards.htm