我应该在XML中使用元素或属性吗? [英] Should I use Elements or Attributes in XML?

查看:102
本文介绍了我应该在XML中使用元素或属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从W3Schools了解有关 XML属性的信息.

作者提到以下内容(强调我的意思):

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属性?

使用属性的一些问题是:

  • 属性不能包含多个值(元素可以)
  • 属性不能包含树结构(元素可以)
  • 属性不容易扩展(以备将来更改)

属性很难读取和维护.使用元素作为数据.使用属性获取与数据无关的信息.

那么作者的观点是著名的观点,还是XML的最佳实践?

应该避免使用XML中的属性吗?

W3Schools还提到了以下内容(强调我的意思):

元数据的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只是一个标识符,用于标识不同的注释.它不是笔记本身的一部分.

我在这里要说的是元数据(有关数据的数据)应存储为属性,而数据本身应存储为元素.

解决方案

属性或元素的使用通常取决于您要建模的数据.

例如,如果某个实体是数据的 PART ,则建议将其设为元素.例如,员工姓名是员工数据的重要组成部分.

现在,如果您要传达有关数据的 METADATA (提供有关数据的其他信息)但又不是真正的数据组成部分,那么最好将其设置为一个属性. 例如,假设每个员工都有一个后端处理所需的GUID,然后将其设置为一个更好的属性(GUID并不是将真正有用的信息传达给查看xml的人的东西,但是对于其他目的可能是必需的)/p>

没有这样的规则说某事应该是属性或元素.

不必不惜一切代价避免属性的使用.有时,元素比元素更容易建模.这实际上取决于您要表示的数据.

I am learning about XML Attributes from W3Schools.

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.

So is the view of the author a famous one, or is this the best practice in XML?

Should Attributes in XML be avoided?

W3Schools also mentioned the following (emphasis mine):

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.

解决方案

Usage of attributes or elements is usually decided by the data you are trying to model.

For instance, if a certain entity is PART of 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.

Now if you want to convey METADATA about 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)

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.

这篇关于我应该在XML中使用元素或属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆