XML 属性与 XML 元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33746/
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
XML attribute vs XML element
提问by Jacob Schoen
At work we are being asked to create XML files to pass data to another offline application that will then create a second XML file to pass back in order to update some of our data. During the process we have been discussing with the team of the other application about the structure of the XML file.
在工作中,我们被要求创建 XML 文件以将数据传递给另一个离线应用程序,然后该应用程序将创建第二个 XML 文件以传回以更新我们的一些数据。在此过程中,我们一直在与其他应用程序的团队讨论 XML 文件的结构。
The sample I came up with is essentially something like:
我想出的样本本质上是这样的:
<INVENTORY>
<ITEM serialNumber="something" location="something" barcode="something">
<TYPE modelNumber="something" vendor="something"/>
</ITEM>
</INVENTORY>
The other team said that this was not industry standard and that attributes should only be used for meta data. They suggested:
另一个团队说这不是行业标准,属性应该只用于元数据。他们建议:
<INVENTORY>
<ITEM>
<SERIALNUMBER>something</SERIALNUMBER>
<LOCATION>something</LOCATION>
<BARCODE>something</BARCODE>
<TYPE>
<MODELNUMBER>something</MODELNUMBER>
<VENDOR>something</VENDOR>
</TYPE>
</ITEM>
</INVENTORY>
The reason I suggested the first is that the size of the file created is much smaller. There will be roughly 80000 items that will be in the file during transfer. Their suggestion in reality turns out to be three times larger than the one I suggested. I searched for the mysterious "Industry Standard" that was mentioned, but the closest I could find was that XML attributes should only be used for meta data, but said the debate was about what was actually meta data.
我建议第一个的原因是创建的文件的大小要小得多。在传输过程中,文件中将有大约 80000 个项目。他们的建议实际上比我建议的要大三倍。我搜索了提到的神秘的“行业标准”,但我能找到的最接近的是 XML 属性应该只用于元数据,但说争论是关于什么是真正的元数据。
After the long winded explanation (sorry) how do you determine what is meta data, and when designing the structure of an XML document how should you decide when to use an attribute or an element?
在冗长的解释(抱歉)之后,您如何确定元数据是什么?在设计 XML 文档的结构时,您应该如何决定何时使用属性或元素?
采纳答案by Chuck
I use this rule of thumb:
我使用这个经验法则:
- An Attribute is something that is self-contained, i.e., a color, an ID, a name.
- An Element is something that does or could have attributes of its own or contain other elements.
- 属性是自包含的东西,即颜色、ID、名称。
- 元素是具有或可能具有自己的属性或包含其他元素的东西。
So yours is close. I would have done something like:
所以你的很接近。我会做这样的事情:
EDIT: Updated the original example based on feedback below.
编辑:根据下面的反馈更新了原始示例。
<ITEM serialNumber="something">
<BARCODE encoding="Code39">something</BARCODE>
<LOCATION>XYX</LOCATION>
<TYPE modelNumber="something">
<VENDOR>YYZ</VENDOR>
</TYPE>
</ITEM>
回答by user44350
Some of the problems with attributes are:
属性的一些问题是:
- attributes cannot contain multiple values (child elements can)
- attributes are not easily expandable (for future changes)
- attributes cannot describe structures (child elements can)
- attributes are more difficult to manipulate by program code
- attribute values are not easy to test against a DTD
- 属性不能包含多个值(子元素可以)
- 属性不容易扩展(以备将来更改)
- 属性不能描述结构(子元素可以)
- 属性更难被程序代码操纵
- 属性值不容易针对 DTD 进行测试
If you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data.
如果您使用属性作为数据的容器,您最终会得到难以阅读和维护的文档。尝试使用元素来描述数据。仅使用属性来提供与数据无关的信息。
Don't end up like this (this is not how XML should be used):
不要像这样结束(这不是应该使用 XML 的方式):
<note day="12" month="11" year="2002"
to="Tove" to2="John" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>
回答by dan04
"XML" stands for "eXtensible MarkupLanguage". A markup language implies that the data is text, marked upwith metadata about structure or formatting.
“XML”代表“可扩展标记语言”。标记语言意味着数据是文本,用有关结构或格式的元数据标记。
XHTML is an example of XML used the way it was intended:
XHTML 是 XML 使用方式的一个例子:
<p><span lang="es">El Jefe</span> insists that you
<em class="urgent">MUST</em> complete your project by Friday.</p>
Here, the distinction between elements and attributes is clear. Text elements are displayed in the browser, and attributes are instructions about howto display them (although there are a few tags that don't work that way).
在这里,元素和属性之间的区别很明显。文本元素显示在浏览器中,属性是关于如何显示它们的说明(尽管有一些标签不能那样工作)。
Confusion arises when XML is used not as a markup language, but as a data serializationlanguage, in which the distinction between "data" and "metadata" is more vague. So the choice between elements and attributes is more-or-less arbitrary except for things that can'tbe represented with attributes (see feenster's answer).
当 XML 不是用作标记语言,而是用作数据序列化语言时,就会出现混淆,其中“数据”和“元数据”之间的区别更加模糊。因此,元素和属性之间的选择或多或少是任意的,除了不能用属性表示的东西(参见 fenster 的回答)。
回答by kjhughes
XML Element vs XML Attribute
XML 元素与 XML 属性
XML is all about agreement. First defer to any existing XML schemas or established conventions within your community or industry.
XML 是关于协议的。 首先遵循您社区或行业内的任何现有 XML 模式或既定约定。
If you are truly in a situation to define your schema from the ground up, here are some general considerations that should inform the element vs attribute decision:
如果您确实需要从头开始定义架构,那么以下是一些应告知元素与属性决策的一般注意事项:
<versus>
<element attribute="Meta content">
Content
</element>
<element attribute="Flat">
<parent>
<child>Hierarchical</child>
</parent>
</element>
<element attribute="Unordered">
<ol>
<li>Has</li>
<li>order</li>
</ol>
</element>
<element attribute="Must copy to reuse">
Can reference to re-use
</element>
<element attribute="For software">
For humans
</element>
<element attribute="Extreme use leads to micro-parsing">
Extreme use leads to document bloat
</element>
<element attribute="Unique names">
Unique or non-unique names
</element>
<element attribute="SAX parse: read first">
SAX parse: read later
</element>
<element attribute="DTD: default value">
DTD: no default value
</element>
</versus>
回答by AnthonyWJones
It may depend on your usage. XML that is used to represent stuctured data generated from a database may work well with ultimately field values being placed as attributes.
这可能取决于您的使用情况。用于表示从数据库生成的结构化数据的 XML 可能适用于最终作为属性放置的字段值。
However XML used as a message transport would often be better using more elements.
然而,用作消息传输的 XML 通常使用更多元素会更好。
For example lets say we had this XML as proposed in the answer:-
例如,假设我们在答案中提出了这个 XML:-
<INVENTORY>
<ITEM serialNumber="something" barcode="something">
<Location>XYX</LOCATION>
<TYPE modelNumber="something">
<VENDOR>YYZ</VENDOR>
</TYPE>
</ITEM>
</INVENTORY>
Now we want to send the ITEM element to a device to print he barcode however there is a choice of encoding types. How do we represent the encoding type required? Suddenly we realise, somewhat belatedly, that the barcode wasn't a single automic value but rather it may be qualified with the encoding required when printed.
现在我们想要将 ITEM 元素发送到设备以打印条形码,但是可以选择编码类型。我们如何表示所需的编码类型?突然,我们意识到,有点晚了,条形码不是一个单一的自动值,而是它可以在打印时使用所需的编码进行限定。
<ITEM serialNumber="something">
<barcode encoding="Code39">something</barcode>
<Location>XYX</LOCATION>
<TYPE modelNumber="something">
<VENDOR>YYZ</VENDOR>
</TYPE>
</ITEM>
The point is unless you building some kind of XSD or DTD along with a namespace to fix the structure in stone, you may be best served leaving your options open.
关键是除非您构建某种 XSD 或 DTD 以及命名空间来固定结构,否则最好保留您的选项。
IMO XML is at its most useful when it can be flexed without breaking existing code using it.
当 IMO XML 可以在不破坏使用它的现有代码的情况下进行伸缩时,它最有用。
回答by Archimedes Trajano
I use the following guidelines in my schema design with regards to attributes vs. elements:
我在我的架构设计中使用以下关于属性与元素的指南:
- Use elements for long running text (usually those of string or normalizedString types)
- Do not use an attribute if there is grouping of two values (e.g. eventStartDate and eventEndDate) for an element. In the previous example, there should be a new element for "event" which may contain the startDate and endDate attributes.
- Business Date, DateTime and numbers (e.g. counts, amount and rate) should be elements.
- Non-business time elements such as last updated, expires on should be attributes.
- Non-business numbers such as hash codes and indices should be attributes.* Use elements if the type will be complex.
- Use attributes if the value is a simple type and does not repeat.
- xml:id and xml:lang must be attributes referencing the XML schema
- Prefer attributes when technically possible.
- 将元素用于长时间运行的文本(通常是字符串或 normalizedString 类型的元素)
- 如果元素有两个值(例如 eventStartDate 和 eventEndDate)的分组,则不要使用属性。在前面的例子中,“event”应该有一个新元素,它可能包含 startDate 和 endDate 属性。
- 营业日期、日期时间和数字(例如计数、金额和比率)应该是元素。
- 非业务时间元素,例如上次更新、过期时间应该是属性。
- 哈希码和索引等非业务数字应该是属性。* 如果类型很复杂,请使用元素。
- 如果值是简单类型且不重复,则使用属性。
- xml:id 和 xml:lang 必须是引用 XML 模式的属性
- 在技术上可能的情况下更喜欢属性。
The preference for attributes is it provides the following:
对属性的偏好是它提供以下内容:
- unique (the attribute cannot appear multiple times)
- order does not matter
- the above properties are inheritable (this is something that the "all" content model does not support in the current schema language)
- bonus is they are less verbose and use up less bandwidth, but that's not really a reason to prefer attributes over elements.
- 唯一(该属性不能多次出现)
- 顺序无关紧要
- 上述属性是可继承的(这是“所有”内容模型在当前模式语言中不支持的)
- 好处是它们不那么冗长,占用的带宽也更少,但这并不是更喜欢属性而不是元素的真正原因。
I added when technically possiblebecause there are times where the use of attributes are not possible. For example, attribute set choices. For example use (startDate and endDate) xor (startTS and endTS) is not possible with the current schema language
我在技术上可行时添加,因为有时无法使用属性。例如,属性集选择。例如,使用(startDate 和 endDate)xor(startTS 和 endTS)在当前模式语言中是不可能的
If XML Schema starts allowing the "all" content model to be restricted or extended then I would probably drop it
如果 XML Schema 开始允许限制或扩展“所有”内容模型,那么我可能会放弃它
回答by peter.murray.rust
There is no universal answer to this question (I was heavily involved in the creation of the W3C spec). XML can be used for many purposes - text-like documents, data and declarative code are three of the most common. I also use it a lot as a data model. There are aspects of these applications where attributes are more common and others where child elements are more natural. There are also features of various tools that make it easier or harder to use them.
这个问题没有统一的答案(我大量参与了 W3C 规范的创建)。XML 可用于多种用途 - 类似文本的文档、数据和声明性代码是最常见的三种。我也经常使用它作为数据模型。在这些应用程序的某些方面,属性更常见,而在其他方面,子元素更自然。还有各种工具的功能,使它们更容易或更难使用。
XHTML is one area where attributes have a natural use (e.g. in class='foo'). Attributes have no order and this may make it easier for some people to develop tools. OTOH attributes are harder to type without a schema. I also find namespaced attributes (foo:bar="zork") are often harder to manage in various toolsets. But have a look at some of the W3C languages to see the mixture that is common. SVG, XSLT, XSD, MathML are some examples of well-known languages and all have a rich supply of attributes and elements. Some languages even allow more-than-one-way to do it, e.g.
XHTML 是属性具有自然用途的一个领域(例如在 class='foo' 中)。属性没有顺序,这可能使某些人更容易开发工具。OTOH 属性在没有模式的情况下更难输入。我还发现命名空间属性 (foo:bar="zork") 在各种工具集中通常更难管理。但是请查看一些 W3C 语言以了解常见的混合。SVG、XSLT、XSD、MathML 是一些知名语言的例子,它们都有丰富的属性和元素。一些语言甚至允许不止一种方式来做到这一点,例如
<foo title="bar"/>;
or
或者
<foo>
<title>bar</title>;
</foo>;
Note that these are NOT equivalent syntactically and require explicit support in processing tools)
请注意,这些在语法上并不等效,并且需要处理工具中的明确支持)
My advice would be to have a look at common practice in the area closest to your application and also consider what toolsets you may wish to apply.
我的建议是查看与您的应用程序最接近的领域的常见做法,并考虑您可能希望应用哪些工具集。
Finally make sure that you differentiate namespaces from attributes. Some XML systems (e.g. Linq) represent namespaces as attributes in the API. IMO this is ugly and potentially confusing.
最后确保将命名空间与属性区分开来。一些 XML 系统(例如 Linq)将命名空间表示为 API 中的属性。IMO 这很丑陋,而且可能令人困惑。
回答by Luke
When in doubt, KISS-- why mix attributes and elements when you don't have a clear reason to use attributes. If you later decide to define an XSD, that will end up being cleaner as well. Then if you even later decide to generate a class structure from your XSD, that will be simpler as well.
如有疑问,KISS——当您没有明确的理由使用属性时,为什么要混合使用属性和元素。如果您稍后决定定义 XSD,那最终也会更清晰。然后,如果您甚至稍后决定从您的 XSD 生成一个类结构,那也会更简单。
回答by Patrick
Others have covered how to differentiate between attributes from elements but from a more general perspective putting everything in attributes because it makes the resulting XML smaller is wrong.
其他人已经介绍了如何区分属性和元素,但从更一般的角度来看,将所有内容都放在属性中是错误的,因为它使生成的 XML 更小。
XML is not designed to be compact but to be portable and human readable. If you want to decrease the size of the data in transit then use something else (such as google's protocol buffers).
XML 不是为了紧凑而设计的,而是为了便于移植和人类可读。如果您想减少传输中数据的大小,请使用其他内容(例如google 的 protocol buffers)。
回答by aku
Both methods for storing object's properties are perfectly valid. You should depart from pragmatic considerations. Try answering following question:
这两种存储对象属性的方法都是完全有效的。你应该远离务实的考虑。尝试回答以下问题:
- Which representation leads to faster data parsing\generation?
- Which representation leads to faster data transfer?
Does readability matter?
...
- 哪种表示会导致更快的数据解析\生成?
- 哪种表示会导致更快的数据传输?
可读性重要吗?
...

