2024-09-15 07:00:01

XML属性vs XML元素

在工作中,我们被要求创建XML文件来将数据传递给另一个脱机应用程序,然后该应用程序将创建第二个XML文件来传递回去,以更新我们的一些数据。在这个过程中,我们一直在与另一个应用程序的团队讨论XML文件的结构。

我提出的样本基本上是这样的:

<INVENTORY>
   <ITEM serialNumber="something" location="something" barcode="something">
      <TYPE modelNumber="something" vendor="something"/> 
   </ITEM>
</INVENTORY>

另一个团队说,这不是行业标准,属性应该只用于元数据。他们建议:

<INVENTORY>
   <ITEM>
      <SERIALNUMBER>something</SERIALNUMBER>
      <LOCATION>something</LOCATION>
      <BARCODE>something</BARCODE>
      <TYPE>
         <MODELNUMBER>something</MODELNUMBER>
         <VENDOR>something</VENDOR>
      </TYPE>
   </ITEM>
</INVENTORY>

我建议使用第一个方法的原因是,创建的文件的大小要小得多。在传输过程中,文件中将有大约80000个项目。事实上,他们的建议比我的建议大三倍。我搜索了提到的神秘的“行业标准”,但我能找到的最接近的是XML属性应该只用于元数据,但争论的焦点是什么才是实际的元数据。

在冗长的解释(抱歉)之后,如何确定什么是元数据,以及在设计XML文档的结构时,如何决定何时使用属性或元素?


当前回答

这个问题没有统一的答案(我曾大量参与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.

<foo title="bar"/>;

or

<foo>
  <title>bar</title>;
</foo>;

注意,它们在语法上是不等价的,需要处理工具的显式支持)

我的建议是,查看与您的应用程序最接近的领域的常见实践,并考虑您可能希望应用什么工具集。

最后,确保将名称空间与属性区分开来。一些XML系统(例如Linq)在API中将名称空间表示为属性。在我看来,这很丑陋,而且可能令人困惑。

其他回答

我的经验是这样的:

属性是自包含的东西,例如颜色、ID、名称。 元素是具有或可能具有自己的属性或包含其他元素的东西。

你的也很接近了。我会这样做:

编辑:根据下面的反馈更新了原始示例。

  <ITEM serialNumber="something">
      <BARCODE encoding="Code39">something</BARCODE>
      <LOCATION>XYX</LOCATION>
      <TYPE modelNumber="something">
         <VENDOR>YYZ</VENDOR>
      </TYPE>
   </ITEM>

在我的模式设计中,我使用了以下关于属性和元素的指导原则:

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.

属性的优先级是它提供了以下内容:

唯一的(该属性不能出现多次) 顺序不重要 上面的属性是可继承的(这是“所有”内容模型在当前模式语言中不支持的) 额外的好处是它们不那么冗长,占用的带宽也更少,但这并不是更喜欢属性而不是元素的真正原因。

我在技术上可能的情况下添加了属性,因为有时不可能使用属性。例如,属性集选择。例如,对于当前的模式语言,使用(startDate和endDate) xor (startTS和endTS)是不可能的

如果XML Schema开始允许限制或扩展“所有”内容模型,那么我可能会放弃它

存储对象属性的两种方法都是完全有效的。你应该放弃实用主义的考虑。试着回答以下问题:

哪种表示方式可以更快地生成数据解析? 哪种表示法可以更快地传输数据? 可读性重要吗? ...

属性的一些问题是:

属性不能包含多个值(子元素可以) 属性不容易扩展(用于将来的更改) 属性不能描述结构(子元素可以) 属性更难以用程序代码操作 属性值不容易根据DTD进行测试

如果您使用属性作为数据的容器,那么您最终会得到难以阅读和维护的文档。尝试使用元素来描述数据。仅在提供与数据无关的信息时使用属性。

不要像这样结束(这不是XML应该使用的方式):

<note day="12" month="11" year="2002" 
      to="Tove" to2="John" from="Jani" heading="Reminder"  
      body="Don't forget me this weekend!"> 
</note>

来源:http://www.w3schools.com/xml/xml_dtd_el_vs_attr.asp

这个问题没有统一的答案(我曾大量参与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.

<foo title="bar"/>;

or

<foo>
  <title>bar</title>;
</foo>;

注意,它们在语法上是不等价的,需要处理工具的显式支持)

我的建议是,查看与您的应用程序最接近的领域的常见实践,并考虑您可能希望应用什么工具集。

最后,确保将名称空间与属性区分开来。一些XML系统(例如Linq)在API中将名称空间表示为属性。在我看来,这很丑陋,而且可能令人困惑。