XML 属性与元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/152313/
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:06:49  来源:igfitidea点击:

XML Attributes vs Elements

xmlschemamodeling

提问by user23726

When should you use XML attributes and when should you use XML elements?

什么时候应该使用 XML 属性,什么时候应该使用 XML 元素?

e.g.

例如

<customData>
  <records>
    <record name="foo" description="bar" />
  </records>
</customData>

or

或者

<customData>
  <records>
    <record>
      <name>foo</name>
      <description>bar</description>
    </record>
  </records>
</customData>

回答by Ryan Taylor

There is an article titled "Principles of XML design: When to use elements versus attributes" on IBM's website.

IBM 网站上有一篇题为“ XML 设计原则:何时使用元素与属性”的文章。

Though there doesn't appear to be many hard and fast rules, there are some good guidelines mentioned in the posting. For instance, one of the recommendations is to use elements when your data must not be normalized for white space as XML processors can normalize data within an attribute thereby modifying the raw text.

虽然似乎没有很多硬性规定,但帖子中提到了一些很好的指导方针。例如,建议之一是在您的数据不得针对空白进行规范化时使用元素,因为 XML 处理器可以规范化属性内的数据,从而修改原始文本。

I find myself referring to this article from time to time as I develop various XML structures. Hopefully this will be helpful to others as well.

我发现自己在开发各种 XML 结构时不时会参考这篇文章。希望这对其他人也有帮助。

edit - From the site:

编辑 - 从网站:

Principle of core content

核心内容原则

If you consider the information in question to be part of the essential material that is being expressed or communicated in the XML, put it in an element. For human-readable documents this generally means the core content that is being communicated to the reader. For machine-oriented records formats this generally means the data that comes directly from the problem domain. If you consider the information to be peripheral or incidental to the main communication, or purely intended to help applications process the main communication, use attributes. This avoids cluttering up the core content with auxiliary material. For machine-oriented records formats, this generally means application-specific notations on the main data from the problem-domain.

如果您认为相关信息是在 XML 中表达或传达的基本材料的一部分,请将其放入元素中。对于人类可读的文档,这通常意味着要传达给读者的核心内容。对于面向机器的记录格式,这通常意味着直接来自问题域的数据。如果您认为信息是主要通信的外围信息或附带信息,或者纯粹用于帮助应用程序处理主要通信,请使用属性。这避免了用辅助材料弄乱核心内容。对于面向机器的记录格式,这通常意味着来自问题域的主要数据上的特定于应用程序的符号。

As an example, I have seen many XML formats, usually home-grown in businesses, where document titles were placed in an attribute. I think a title is such a fundamental part of the communication of a document that it should always be in element content. On the other hand, I have often seen cases where internal product identifiers were thrown as elements into descriptive records of the product. In some of these cases, attributes were more appropriate because the specific internal product code would not be of primary interest to most readers or processors of the document, especially when the ID was of a very long or inscrutable format.

例如,我见过许多 XML 格式,通常是企业内部开发的,其中文档标题被放置在一个属性中。我认为标题是文档交流的一个基本部分,它应该始终位于元素内容中。另一方面,我经常看到将内部产品标识符作为元素放入产品描述性记录中的情况。在其中一些情况下,属性更合适,因为特定的内部产品代码对文档的大多数读者或处理者来说不是主要兴趣,尤其是当 ID 的格式很长或难以理解时。

You might have heard the principle data goes in elements, metadata in attributes. The above two paragraphs really express the same principle, but in more deliberate and less fuzzy language.

您可能听说过原则数据在元素中,元数据在属性中。上面两段确实表达了同样的原则,只是用了更深思熟虑、更少模糊的语言。

Principle of structured information

结构化信息原理

If the information is expressed in a structured form, especially if the structure may be extensible, use elements. On the other hand: If the information is expressed as an atomic token, use attributes. Elements are the extensible engine for expressing structure in XML. Almost all XML processing tools are designed around this fact, and if you break down structured information properly into elements, you'll find that your processing tools complement your design, and that you thereby gain productivity and maintainability. Attributes are designed for expressing simple properties of the information represented in an element. If you work against the basic architecture of XML by shoehorning structured information into attributes you may gain some specious terseness and convenience, but you will probably pay in maintenance costs.

如果信息以结构化形式表达,特别是如果结构可以扩展,请使用元素。另一方面:如果信息表示为原子标记,请使用属性。元素是在 XML 中表达结构的可扩展引擎。几乎所有 XML 处理工具都是围绕这一事实设计的,如果您将结构化信息正确地分解为元素,您会发现您的处理工具补充了您的设计,从而提高了生产力和可维护性。属性旨在表达元素中表示的信息的简单属性。如果您通过将结构化信息硬塞进属性来反对 XML 的基本体系结构,您可能会获得一些似是而非的简洁性和便利性,但您可能会付出维护成本。

Dates are a good example: A date has fixed structure and generally acts as a single token, so it makes sense as an attribute (preferably expressed in ISO-8601). Representing personal names, on the other hand, is a case where I've seen this principle surprise designers. I see names in attributes a lot, but I have always argued that personal names should be in element content. A personal name has surprisingly variable structure (in some cultures you can cause confusion or offense by omitting honorifics or assuming an order of parts of names). A personal name is also rarely an atomic token. As an example, sometimes you may want to search or sort by a forename and sometimes by a surname. I should point out that it is just as problematic to shoehorn a full name into the content of a single element as it is to put it in an attribute.

日期就是一个很好的例子:日期具有固定的结构,通常充当单个标记,因此作为属性(最好用 ISO-8601 表示)是有意义的。另一方面,代表个人姓名是我看到这个原则让设计师感到惊讶的一个例子。我经常在属性中看到名字,但我一直认为个人名字应该在元素内容中。人名具有惊人的可变结构(在某些文化中,您可能会通过省略敬语或假设名称各部分的顺序而引起混淆或冒犯)。个人名称也很少是原子标记。例如,有时您可能希望按名字搜索或排序,有时按姓氏进行搜索或排序。我应该指出,将全名硬塞到单个元素的内容中与将其放在属性中一样有问题。

回答by skaffman

One of the better thought-out element vs attribute arguments comes from the UK GovTalk guidelines. This defines the modelling techniques used for government-related XML exchanges, but it stands on its own merits and is worth considering.

更好的考虑元素与属性参数之一来自英国 GovTalk 指南。这定义了用于与政府相关的 XML 交换的建模技术,但它有其自身的优点,值得考虑。

Schemas MUST be designed so that elements are the main holders of information content in the XML instances. Attributes are more suited to holding ancillary metadata – simple items providing more information about the element content. Attributes MUST NOT be used to qualify other attributes where this could cause ambiguity.

Unlike elements, attributes cannot hold structured data. For this reason, elements are preferred as the principal holders of information content. However, allowing the use of attributes to hold metadata about an element's content (for example, the format of a date, a unit of measure or the identification of a value set) can make an instance document simpler and easier to understand.

A date of birth might be represented in a message as:

模式的设计必须使元素成为 XML 实例中信息内容的主要持有者。属性更适合保存辅助元数据——提供有关元素内容的更多信息的简单项目。属性不得用于限定可能导致歧义的其他属性。

与元素不同,属性不能保存结构化数据。出于这个原因,元素被首选作为信息内容的主要持有者。但是,允许使用属性来保存有关元素内容的元数据(例如,日期格式、度量单位或值集的标识)可以使实例文档更简单、更易于理解。

出生日期可能在消息中表示为:

 <DateOfBirth>1975-06-03</DateOfBirth> 

However, more information might be required, such as how that date of birth has been verified. This could be defined as an attribute, making the element in a message look like:

但是,可能需要更多信息,例如如何验证出生日期。这可以定义为一个属性,使消息中的元素看起来像:

<DateOfBirth VerifiedBy="View of Birth Certificate">1975-06-03</DateOfBirth> 

The following would be inappropriate:

以下情况是不合适的:

<DateOfBirth VerifiedBy="View of Birth Certificate" ValueSet="ISO 8601" Code="2">1975-06-03</DateOfBirth>   

It is not clear here whether the Code is qualifying the VerifiedBy or the ValueSet attribute. A more appropriate rendition would be:

这里不清楚代码是限定 VerifiedBy 还是 ValueSet 属性。更合适的演绎是:

 <DateOfBirth>    
   <VerifiedBy Code="2">View of Birth Certificate</VerifiedBy>     
   <Value ValueSet="ISO 8601">1975-06-03</Value>
 </DateOfBirth>

回答by Jon Skeet

Personally I like using attributes for simple single-valued properties. Elements are (obviously) more suitable for complex types or repeated values.

我个人喜欢将属性用于简单的单值属性。元素(显然)更适合复杂类型或重复值。

For single-valued properties, attributes lead to more compact XML and simpler addressing in most APIs.

对于单值属性,属性导致在大多数 API 中更紧凑的 XML 和更简单的寻址。

回答by Rory Becker

It's largely a matter of preference. I use Elements for grouping and attributes for data where possible as I see this as more compact than the alternative.

这在很大程度上是一个偏好问题。我尽可能使用元素进行分组和数据属性,因为我认为这比替代方案更紧凑。

For example I prefer.....

例如我更喜欢......

<?xml version="1.0" encoding="utf-8"?>
<data>
    <people>
        <person name="Rory" surname="Becker" age="30" />
        <person name="Travis" surname="Illig" age="32" />
        <person name="Scott" surname="Hanselman" age="34" />
    </people>
</data>

...Instead of....

...代替....

<?xml version="1.0" encoding="utf-8"?>
<data>
    <people>
        <person>
            <name>Rory</name>
            <surname>Becker</surname>
            <age>30</age>
        </person>
        <person>
            <name>Travis</name>
            <surname>Illig</surname>
            <age>32</age>
        </person>
        <person>
            <name>Scott</name>
            <surname>Hanselman</surname>
            <age>34</age>
        </person>
    </people>
</data>

However if I have data which does not represent easily inside of say 20-30 characters or contains many quotes or other characters that need escaping then I'd say it's time to break out the elements... possibly with CData blocks.

但是,如果我的数据在 20-30 个字符内不容易表示,或者包含许多引号或其他需要转义的字符,那么我会说是时候打破元素了......可能使用 CData 块。

<?xml version="1.0" encoding="utf-8"?>
<data>
    <people>
        <person name="Rory" surname="Becker" age="30" >
            <comment>A programmer whose interested in all sorts of misc stuff. His Blog can be found at http://rorybecker.blogspot.com and he's on twitter as @RoryBecker</comment>
        </person>
        <person name="Travis" surname="Illig" age="32" >
            <comment>A cool guy for who has helped me out with all sorts of SVn information</comment>
        </person>
        <person name="Scott" surname="Hanselman" age="34" >
            <comment>Scott works for MS and has a great podcast available at http://www.hanselminutes.com </comment>
        </person>
    </people>
</data>

回答by Dan

As a general rule, I avoid attributes altogether. Yes, attributes are more compact, but elements are more flexible, and flexibility is one of the most important advantages of using a data format like XML. What is a single value today can become a list of values tomorrow.

作为一般规则,我完全避免使用属性。是的,属性更紧凑,但元素更灵活,灵活性是使用 XML 等数据格式的最重要优势之一。今天的单个值明天可能会变成一个值列表。

Also, if everything's an element, you never have to remember how you modeled any particular bit of information. Not using attributes means you have one less thing to think about.

此外,如果一切都是一个元素,您永远不必记住您是如何对任何特定信息进行建模的。不使用属性意味着您可以少考虑一件事。

回答by Dhaust

Check out Elements vs. attributesby Ned Batchelder.

查看Ned Batchelder 的Elements vs. attributes

Nice explanation and a good list of the benefits and disadvantages of Elements and Attributes.

很好的解释和元素和属性的优缺点列表。

He boils it down to:

他将其归结为:

Recommendation: Use elements for data that will be produced or consumed by a business application, and attributes for metadata.

建议:将元素用于将由业务应用程序生成或使用的数据,并为元数据使用属性。

Important: Please see @maryisdead's comment below for further clarification.

重要提示:请参阅下面@maryisdead 的评论以进一步说明。

回答by Robert Rossney

The limitations on attributes tell you where you can and can't use them: the attribute names must be unique, their order cannot be significant, and both the name and the value can contain only text. Elements, by contrast, can have non-unique names, have significant ordering, and can have mixed content.

属性的限制告诉您可以和不可以使用它们的位置:属性名称必须是唯一的,它们的顺序不能重要,并且名称和值都只能包含文本。相比之下,元素可以具有不唯一的名称、具有重要的顺序,并且可以具有混合内容。

Attributes are usable in domains where they map onto data structures that follow those rules: the names and values of properties on an object, of columns in a row of a table, of entries in a dictionary. (But not if the properties aren't all value types, or the entries in the dictionary aren't strings.)

属性可用于映射到遵循这些规则的数据结构的域中:对象属性的名称和值、表格行中的列、字典中的条目。(但如果属性不是所有值类型,或者字典中的条目不是字符串,则不是。)

回答by Calmarius

My personal rule of thumb: if an element can contain only one of that thing, and its an atomic data (id, name, age, type, etc...) it should be an attribute otherwise an element.

我个人的经验法则:如果一个元素只能包含一个元素,并且它是一个原子数据(id、名称、年龄、类型等),那么它应该是一个属性,否则是一个元素。

回答by dommer

I tend to use elements when it's data that a human reader would need to know and attributes when it's only for processing (e.g. IDs). This means that I rarely use attributes, as the majority of the data is relevant to the domain being modeled.

当它是人类读者需要知道的数据时,我倾向于使用元素,而当它仅用于处理(例如 ID)时,我倾向于使用元素。这意味着我很少使用属性,因为大部分数据都与正在建模的域相关。

回答by Iz.

Here is another strategy that can help distinguishing elements from attributes: think of objects and keep in mind MVC.

这是另一个有助于区分元素和属性的策略:考虑对象并记住 MVC。

Objects can have members (object variables) and properties (members with setters and getters). Properties are highly useful with MVC design, allowing change notification mechanism.

对象可以有成员(对象变量)和属性(具有 setter 和 getter 的成员)。属性对于 MVC 设计非常有用,允许更改通知机制。

If this is the direction taken, attributes will be used for internal application data that cannot be changed by the user; classic examples will be ID or DATE_MODIFIED. Elements will therefore be used to data that can be modified by users.

如果这是所采取的方向,则属性将用于用户无法更改的内部应用程序数据;经典示例将是 ID 或 DATE_MODIFIED。因此,元素将用于用户可以修改的数据。

So the following would make sense considering the librarian first add a book item (or a magazine), and then can edit its name author ISBN etc:

因此,考虑到图书管理员首先添加一本书(或一本杂志),然后可以编辑其名称作者 ISBN 等,以下内容是有意义的:

<?xml version="1.0" encoding="utf-8"?>
<item id="69" type="book">
    <authors count="1">
        <author>
            <name>John Smith</name>
        <author>
    </authors>
    <ISBN>123456790</ISBN>
</item>