我应该在 XML 中使用元素还是属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1096797/
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
Should I use Elements or Attributes in XML?
提问by Ibn Saeed
I am learning about XML Attributes from W3Schools.
我正在从 W3Schools学习XML 属性。
The author mentions the following (emphasis mine):
作者提到了以下内容(强调我的):
XML Elements vs. Attributes
<person sex="female"> <firstname>Anna</firstname> <lastname>Smith</lastname> </person><person> <sex>female</sex> <firstname>Anna</firstname> <lastname>Smith</lastname> </person>In the first example sex is an attribute. In the last, sex is an element. Both examples provide the same information.
There are no rules about when to use attributes and when to use elements. Attributes are handy in HTML. In XML my advice is to avoid them. Use elements instead.
Avoid XML Attributes?
Some of the problems with using attributes are:
- attributes cannot contain multiple values (elements can)
- attributes cannot contain tree structures (elements can)
- attributes are not easily expandable (for future changes)
Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data.
XML 元素与属性
<person sex="female"> <firstname>Anna</firstname> <lastname>Smith</lastname> </person><person> <sex>female</sex> <firstname>Anna</firstname> <lastname>Smith</lastname> </person>在第一个示例中,性别是一个属性。最后,性是一个元素。这两个示例提供相同的信息。
关于何时使用属性和何时使用元素没有规则。属性在 HTML 中很方便。在 XML 中,我的建议是避免使用它们。改用元素。
避免使用 XML 属性?
使用属性的一些问题是:
- 属性不能包含多个值(元素可以)
- 属性不能包含树结构(元素可以)
- 属性不容易扩展(以备将来更改)
属性难以阅读和维护。将元素用于数据。对与数据无关的信息使用属性。
So is the view of the author a famous one, or is this the best practice in XML?
那么作者的观点是著名的观点,还是 XML 中的最佳实践?
Should Attributes in XML be avoided?
应该避免使用 XML 中的属性吗?
W3Schools also mentioned the following (emphasis mine):
W3Schools 还提到了以下内容(重点是我的):
XML Attributes for Metadata
Sometimes ID references are assigned to elements. These IDs can be used to identify XML elements in much the same way as the ID attribute in HTML. This example demonstrates this:
<messages> <note id="501"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> <note id="502"> <to>Jani</to> <from>Tove</from> <heading>Re: Reminder</heading> <body>I will not</body> </note> </messages>The ID above is just an identifier, to identify the different notes. It is not a part of the note itself.
What I'm trying to say here is that metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.
元数据的 XML 属性
有时 ID 引用被分配给元素。这些 ID 可用于标识 XML 元素,其方式与 HTML 中的 ID 属性大致相同。此示例演示了这一点:
<messages> <note id="501"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> <note id="502"> <to>Jani</to> <from>Tove</from> <heading>Re: Reminder</heading> <body>I will not</body> </note> </messages>上面的ID只是一个标识符,用来识别不同的音符。它不是笔记本身的一部分。
我在这里想说的是元数据(关于数据的数据)应该存储为属性,而数据本身应该存储为元素。
采纳答案by Prashanth
Usage of attributes or elements is usually decided by the data you are trying to model.
属性或元素的使用通常由您尝试建模的数据决定。
For instance, if a certain entity is PARTof the data, then it is advisable to make it an element. For example the name of the employee is an essential part of the employee data.
例如,如果某个实体是PART数据,则最好是使一个元素。例如,员工姓名是员工数据的重要组成部分。
Now if you want to convey METADATAabout data (something that provides additional information about the data) but is not really part of the data, then it is better to make it an attribute. For instance, lets say each employee has a GUID needed for back end processing, then making it an attribute is better.(GUID is not something that conveys really useful information to someone looking at the xml, but might be necessary for other purposes)
现在,如果您想传达有关数据的METADATA(提供有关数据的附加信息的东西)但不是数据的真正组成部分,那么最好将其设为属性。例如,假设每个员工都有一个后端处理所需的 GUID,然后将其设置为一个属性会更好。(GUID 不是向查看 xml 的人传达真正有用的信息的东西,但可能出于其他目的而需要)
There is no rule as such that says something should be an attribute or a element.
没有这样的规则说某物应该是属性或元素。
Its not necessary to AVOID attributes at all costs..Sometimes they are easier to model, than elements. It really depends on the data you are trying to represent.
没有必要不惜一切代价避免属性......有时它们比元素更容易建模。这实际上取决于您尝试表示的数据。
回答by William Walseth
My 0.02 five years after the OP is the exact opposite. Let me explain.
我在 OP 五年后的 0.02 正好相反。让我解释。
- Use elements when you're grouping similar data, and attributes of that data.
- Don't use elements for everything.
- If the data repeats (1 to many), it's probably an element
- If the data never repeats, and only makes sense when correlated to something else, it's an attribute.
- If data doesn't have other attributes (i.e. a name), then it's an attribute
- Group like elements together to support collection parsing (i.e. /xml/character)
- Re-use similar element names to support parsing data
- Never, ever, use numbers in element names to show position. (i.e. character1, character2) This practice makes it very hard to parse (see #6, parsing code must /character1, /character2, etc. not simply /character.
- 在对相似数据和该数据的属性进行分组时使用元素。
- 不要对所有事情都使用元素。
- 如果数据重复(1 对多),则可能是一个元素
- 如果数据从不重复,并且只有在与其他事物相关时才有意义,那么它就是一个属性。
- 如果数据没有其他属性(即名称),那么它就是一个属性
- 将相似元素组合在一起以支持集合解析(即 /xml/character)
- 重用相似的元素名称来支持解析数据
- 永远,永远,使用数字的元素名称显示位置。(即character1、character2)这种做法使得解析非常困难(见#6,解析代码必须是/character1、/character2等,而不是简单的/character。
Considered another way:
考虑另一种方式:
- Start by thinking of allyour data as an attribute.
- Logically group attributes into elements. If you know your data, you'll rarely need to convert attribute to an element. You probably already know when an element (collection, or repeated data) is necessary
- Group elements together logically
- When you run into the case the you need to expand, add new elements / attributes based on the logical structure an process above. Adding a new collection of child elements won't "break" your design, and will be easier to read over time.
- 首先将所有数据视为一个属性。
- 将属性按逻辑分组到元素中。如果您了解自己的数据,则很少需要将属性转换为元素。您可能已经知道何时需要元素(集合或重复数据)
- 逻辑地将元素组合在一起
- 当您遇到需要扩展的情况时,根据上述流程的逻辑结构添加新元素/属性。添加新的子元素集合不会“破坏”您的设计,并且随着时间的推移会更容易阅读。
For example, looking at a simple collection of books and major characters, the title won't ever have "children", it's a simple element. Every character has a name and age.
例如,看一个简单的书籍和主要人物的集合,标题永远不会有“孩子”,这是一个简单的元素。每个角色都有名字和年龄。
<book title='Hitchhiker's Guide to the Galaxy' author='Douglas Adams'>
<character name='Zaphod Beeblebrox' age='100'/>
<character name='Arthur Dent' age='42'/>
<character name='Ford Prefect' age='182'/>
</book>
<book title='On the Road' author='Hyman Kerouac'>
<character name='Dean Moriarty' age='30'/>
<character name='Old Bull Lee' age='42'/>
<character name='Sal Paradise' age='42'/>
</book>
You could argue that a book could have multiple authors. OK, just expand by adding new author elements (optionally remove the original @author). Sure, you've broken the original structure, but in practice it's pretty rare, and easy to work around. Any consumer of your original XML that assumed a single author will have to change anyway (they are likely changing their DB to move author from a column in the 'book' table to an 'author' table).
你可以争辩说一本书可以有多个作者。好的,只需通过添加新的 author 元素来扩展(可选地删除原始的 @author)。当然,您已经破坏了原始结构,但实际上这种情况很少见,而且很容易解决。任何假设单个作者的原始 XML 使用者无论如何都必须更改(他们可能会更改他们的数据库以将作者从“书”表中的列移动到“作者”表)。
<book title='Hitchhiker's Guide to the Galaxy'>
<author name='Douglas Adams'/>
<author name='Some Other Guy'/>
<character name='Zaphod Beeblebrox' age='100'/>
<character name='Arthur Dent' age='42'>
<character name='Ford Prefect' age='182'/>
</book>
回答by flybywire
Not least important is that putting things in attributes makes for less verbose XML.
同样重要的是,将事物放入属性中可以减少 XML 的冗长。
Compare
相比
<person name="John" age="23" sex="m"/>
Against
反对
<person>
<name>
John
</name>
<age>
<years>
23
</years>
</age>
<sex>
m
</sex>
</person>
Yes, that was a little biased and exaggerated, but you get the point
是的,这有点偏颇和夸大,但你明白了
回答by Gajus
I've used Google to search for the exact question. First I landed on this article, http://www.ibm.com/developerworks/library/x-eleatt/index.html. Though, it felt too long for a simple question as such. Anyhow, I've read through all the answers on this topic and didn't find a satisfactory summary. As such, I went back to the latter article. Here is a summary:
我已经使用 Google 搜索了确切的问题。我首先阅读了这篇文章,http://www.ibm.com/developerworks/library/x-eleatt/index.html。不过,像这样一个简单的问题感觉太长了。无论如何,我已经阅读了有关该主题的所有答案,但没有找到令人满意的总结。因此,我回到了后一篇文章。这是一个总结:
When do I use elements and when do I use attributes for presenting bits of information?
什么时候使用元素,什么时候使用属性来呈现信息?
- If the information in question could be itself marked up with elements, put it in an element.
- If the information is suitable for attribute form, but could end up as multiple attributes of the same name on the same element, use child elements instead.
- If the information is required to be in a standard DTD-like attribute type such as ID, IDREF, or ENTITY, use an attribute.
- If the information should not be normalized for white space, use elements. (XML processors normalize attributesin ways that can change the raw text of the attribute value.)
- 如果相关信息本身可以用元素标记,请将其放入元素中。
- 如果信息适用于实体形式,但最终可能会在同一元素上出现多个同名属性,请改用子元素。
- 如果要求信息采用标准的类似 DTD 的属性类型,例如 ID、IDREF 或 ENTITY,请使用属性。
- 如果信息不应该针对空白进行标准化,请使用元素。(XML 处理器以可以更改属性值的原始文本的方式规范化属性。)
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. 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.
如果您认为相关信息是在 XML 中表达或传达的基本材料的一部分,请将其放入元素中。如果您认为信息是主要通信的外围信息或附带信息,或者纯粹用于帮助应用程序处理主要通信,请使用属性。
Principle of structured information
结构化信息原理
If the information is expressed in a structured form, especially if the structure may be extensible, use elements. If the information is expressed as an atomic token, use attributes.
如果信息以结构化形式表达,特别是如果结构可以扩展,请使用元素。如果信息表示为原子标记,请使用属性。
Principle of readability
可读性原则
If the information is intended to be read and understood by a person, use elements. If the information is most readily understood and digested by a machine, use attributes.
如果信息旨在供人阅读和理解,请使用元素。如果机器最容易理解和消化信息,请使用属性。
Principle of element/attribute binding
元素/属性绑定原理
Use an element if you need its value to be modified by another attribute. [..] it is almost always a terrible idea to have one attribute modify another.
如果您需要一个元素的值被另一个属性修改,请使用该元素。[..] 让一个属性修改另一个属性几乎总是一个糟糕的主意。
This is a short summary of the important bits from the article. If you wish to see examples and full description of every case, then refer to the original article.
这是文章中重要部分的简短摘要。如果您希望查看每个案例的示例和完整描述,请参阅原始文章。
回答by Robert Rossney
Attributes model mapping. A set of attributes on an element isomorphizes directly onto a name/value map in which the values are text or any serializable value type. In C#, for instance, any Dictionary<string, string>object can be represented as an XML attribute list, and vice versa.
属性模型映射。元素上的一组属性直接同构到名称/值映射上,其中值是文本或任何可序列化的值类型。例如,在 C# 中,任何Dictionary<string, string>对象都可以表示为 XML 属性列表,反之亦然。
This is emphatically not the case with elements. While you can always transform a name/value map into a set of elements, the reverse is not the case, e.g.:
这显然不是元素的情况。虽然您始终可以将名称/值映射转换为一组元素,但情况并非如此,例如:
<map>
<key1>value</key1>
<key1>another value</key1>
<key2>a third value</key2>
</map>
If you transform this into a map, you'll lose two things: the multiple values associated with key1, and the fact that key1appears before key2.
如果将其转换为地图,则会丢失两件事:与 关联的多个值key1,以及key1出现在 之前的事实key2。
The significance of this becomes a lot clearer if you look at DOM code that's used to update information in a format like this. For instance, it's trivial to write this:
如果您查看用于以此类格式更新信息的 DOM 代码,则其重要性会变得更加清晰。例如,写这个很简单:
foreach (string key in map.Keys)
{
mapElement.SetAttribute(key, map[key]);
}
That code is concise and unambiguous. Contrast it with, say:
该代码简洁明了。对比一下,说:
foreach (string key in map.Keys)
{
keyElement = mapElement.SelectSingleNode(key);
if (keyElement == null)
{
keyElement = mapElement.OwnerDocument.CreateElement(key);
mapElement.AppendChild(keyElement);
}
keyElement.InnerText = value;
}
回答by Anthony Scaife
This is an example where attributes are data about data.
这是一个示例,其中属性是关于数据的数据。
Databases are named by their ID attribute.
数据库由它们的 ID 属性命名。
The "type" attribute of the database denotes what is expected to be found inside the database tag.
数据库的“类型”属性表示预期在数据库标签内找到的内容。
<databases>
<database id='human_resources' type='mysql'>
<host>localhost</host>
<user>usrhr</user>
<pass>jobby</pass>
<name>consol_hr</name>
</database>
<database id='products' type='my_bespoke'>
<filename>/home/anthony/products.adb</filename>
</database>
</databases>
回答by Coxy
You can't put a CDATA in an attribute. In my experience, sooner or later you are going to want to put single quotes, double quotes and/or entire XML documents into a "member", and if it's an attribute you're going to be cursing at the person who used attributes instead of elements.
您不能将 CDATA 放在属性中。根据我的经验,您迟早会想要将单引号、双引号和/或整个 XML 文档放入“成员”中,如果它是一个属性,您将诅咒使用属性的人的元素。
Note: my experience with XML mainly involved cleaning up other peoples'. These people seemed to follow the old adage "XML is like violence. If using it hasn't solved your problem, then you haven't used enough."
注意:我在 XML 方面的经验主要涉及清理其他人的。这些人似乎遵循了一句古老的格言“XML 就像暴力。如果使用它还没有解决您的问题,那么您还没有使用足够的东西”。
回答by Pavel Minaev
It all depends on what XML is used for. When it's mostly interop between software and machines - such as Web services it's easier to go all-elements if only for the sake of consistency (and also some frameworks prefer it that way, e.g. WCF). If it is targeted for human consumption - i.e. primarily created and/or read by people - then judicious use of attributes can improve readability quite a lot; XHTML is a reasonable example of that, and also XSLT and XML Schema.
这完全取决于 XML 的用途。当它主要是软件和机器之间的互操作时——比如 Web 服务,如果只是为了一致性,那么使用所有元素会更容易(而且一些框架更喜欢这种方式,例如 WCF)。如果它是针对人类消费的——即主要由人们创建和/或阅读——那么明智地使用属性可以大大提高可读性;XHTML 是一个合理的例子,还有 XSLT 和 XML 模式。
回答by Brian Agnew
I usually work on the basis that attributes are metadata- that is, data about the data. One thing I do avoid is putting lists in attributes. e.g.
我通常在属性是元数据的基础上工作- 即关于数据的数据。我避免的一件事是将列表放在属性中。例如
attribute="1 2 3 7 20"
Otherwise you have an extra level of parsing to extract each element. If XML provides the structure and tools for lists, then why impose another yourself.
否则,您需要额外的解析级别来提取每个元素。如果 XML 为列表提供了结构和工具,那为什么还要自己强加另一个。
One scenario where you may want to code in preference for attributes is for processing speed via a SAX parser. Using a SAX parser you will get an element call back containing the element name and the list of attributes. If you had used multiple elements instead then you'll get multiple callbacks (one for each element). How much of a burden/timesink this is is up for debate of course, but perhaps worth considering.
您可能希望优先针对属性进行编码的一种情况是通过 SAX 解析器提高处理速度。使用 SAX 解析器,您将获得一个包含元素名称和属性列表的元素回调。如果您使用了多个元素,那么您将获得多个回调(每个元素一个)。当然,这有多少负担/时间有待讨论,但也许值得考虑。
回答by John Saunders
The author's points are correct (except that attributes may contain a list of values). The question is whether or not you care about his points.
作者的观点是正确的(除了属性可能包含值列表)。问题是你是否关心他的观点。
It's up to you.
由你决定。

