最佳实践:XML属性vs XML元素——什么时候应该使用元素,什么时候应该使用属性?(复制)

时间:2022-09-10 22:58:53

This question already has an answer here:

这个问题已经有了答案:

Which would be the correct format for this XML data, are they equivalent or are there trade offs between the two?

这个XML数据的正确格式是什么,它们是等价的还是两者之间有权衡?

1.

1。

<sitemap>
  <category name="Animals">
    <section title="Dogs">
      <page url="/pics/greatdane.jpg" title="Great Dane"/>
    </section>
  </category>
</sitemap>

2.

2。

<sitemap>
  <page>
    <category>Animals</category>
    <section>Dogs</section>
    <title>Great Dane</title>
    <url>/pics/greatdane.jpg</url>    
  </page>
</sitemap>

I've implemented the first example with my style sheet and it seems to work fine, but I'm unsure what the correct form should be.

我已经用样式表实现了第一个示例,它似乎可以正常工作,但是我不确定正确的形式应该是什么。

10 个解决方案

#1


20  

The issue of attributes vs elements has been around for the better part of a decade and there is no right answer. Instead consider the differences and from that you should be able to decide which to use:

关于属性vs元素的问题已经存在了近十年,而且没有正确的答案。相反,你应该考虑不同之处,从中你应该能够决定使用哪一种:

  • There can be only one instance of an attribute although you can enforce this with elements using DTD or XML Schema;
  • 虽然可以使用DTD或XML模式对元素进行强制执行,但也只能有一个属性实例;
  • Attributes are unordered. Elements are not;
  • 属性是无序的。元素就不明智了;
  • Attributes lead to a more concise syntax if there are no children. Compare:

    如果没有子属性,属性将导致更简洁的语法。比较:

    <page name="Sitemap"/>

    页面< name = "地图" / >

to:

:

<page>
  <name>Sitemap</name>
</page>

I know which one I prefer;

我知道我更喜欢哪个;

  • Not really relevant now since DTDs aren't used much (if at all) over XML Schema but I'll add it anyway: DTDs allow default values (implied) for attributes but no such mechanism for elements; and
  • 由于dtd没有在XML模式中大量使用(如果有的话),所以现在并不是很相关,但是我还是要添加它:dtd允许属性的默认值(隐含值),但是对于元素没有这样的机制;和
  • Elements, being elementss, can have children and attributes of their own. Attributes obviously cannot.
  • 元素作为元素,可以有自己的子元素和属性。属性显然不能。

So, from your example, your innermost <page> element has a URL attribute (although it's an image for some reason--perhaps a preview icon? If so the attribute name is misleading). A webpage only has one URL (generally) so that'd be a good example of something that could be an attribute.

因此,从您的示例中,最内层的 元素有一个URL属性(尽管出于某种原因它是一个图像——也许是一个预览图标?)如果是这样,属性名就具有误导性)。一个网页(通常)只有一个URL,这是一个很好的例子,可以是一个属性。

If on the other hand you wanted to list the images on the page, there could obviously be more than one so you'd need elements for that.

另一方面,如果你想要在页面上列出图片,显然会有不止一个,所以你需要一些元素。

But, in the end, most of the time there's no right or wrong answer and it's largely a question of style.

但是,最后,大多数时候没有正确或错误的答案,这很大程度上是一个风格问题。

#2


12  

The two examples are not equivalent, because they form different hierarchies. Is a sitemap a list of categories, like the first example? Or is it a list of pages like the second example?

这两个例子并不相同,因为它们形成了不同的层次结构。站点地图是一个类别列表吗?或者是像第二个例子那样的页面列表?

The answer to that is orthogonal to the element vs attribute question.

这个问题的答案与元素vs属性问题是正交的。

On the Element vs Attribute question: Here is your second example transformed to an attribute approach:

关于元素vs属性的问题:这是您将第二个示例转换为属性方法的例子:

<sitemap>
 <page    
  category='Animals'
  section='Dogs'
  title='Great Dane'
  url='/pics/greatdane.jpg'
  /> 
</sitemap>

The above and your second case are equivalent. One consideration for choosing one versus the other is based on whether you may modify the schema in the future. Adding an attribute to the url element as in the following example would likely be a backward compatable change. The semantically same modification would be impossible in the attribute approach, as you cannot attach an attribute to an attribute.

以上两种情况是等价的。选择一个与另一个的考虑是基于您是否可以在将来修改模式。向url元素添加属性(如下例所示)可能是一种向后可压缩的更改。在属性方法中,语义相同的修改是不可能的,因为您不能将属性附加到属性。

<sitemap>
 <page>    
  <category>Animals</category>
  <section>Dogs</section>    
  <title>Great Dane</title>    
  <url nofollow="true">/pics/greatdane.jpg</url>
 </page> 
</sitemap>

#3


3  

I think that the answer is quite obvious when you think about how you want to add more dogs:

我认为答案是很明显的当你想要增加更多的狗:

<sitemap>
  <category name="Animals">
    <section title="Dogs">
      <page url="/pics/greatdane.jpg" title="Great Dane"/>
      <page url="/pics/wienerdog.jpg" title="Wiener Dog"/>
    </section>
  </category>
</sitemap>

or

<sitemap>
  <page>
    <category>Animals</category>
    <section>Dogs</section>
    <title>Great Dane</title>
    <url>/pics/greatdane.jpg</url>    
  </page>
  <page>
    <category>Animals</category>
    <section>Dogs</section>
    <title>Wiener Dog</title>
    <url>/pics/wienerdog.jpg</url>
  </page>
</sitemap>

#4


2  

There aren't usually right or wrong answers for this sort of thing. Mostly it depends on how you need to access your data.

这类事情通常没有正确或错误的答案。这主要取决于您如何访问您的数据。

One nice thing about the first one is that it easily, perhaps later, supports multiple pages in a section and multiple sections in a category. In the second one that information is spread across pages.

第一个优点是,它很容易在一个类别中支持多个页面和多个部分。在第二种方法中,信息被分散在不同的页面上。

#5


2  

XML is horrible file format and ends up in religious wars. Do whatever you feel is best at the time, provided you can justify it. However, your particular examples are significantly different:

XML是一种糟糕的文件格式,最终导致宗教战争。只要你能证明它的合理性,就做你认为最好的事情。然而,你的具体例子有很大的不同:

In 1, sitemap encapsulates a category encapsulating a section which encapsulates the page.

在1中,sitemap封装了封装页面的部分的类别。

In 2, sitemap encapsulates a page which encapsulates three items: category, section, title and URL. None of these three items contains any other, they are siblings, all commonly held within the page.

在2中,sitemap封装了一个页面,该页面封装了三个条目:类别、部分、标题和URL。这三个项目中没有一个包含其他的,它们都是同级的,都是页面中常见的。

As two different structures, it depends on which you intended.

作为两个不同的结构,它取决于你想要哪个。

A different question would be the preference for attributes versus first-level tags. But as I say, that is a different question!

另一个问题是对属性和一级标签的偏好。但正如我所说,这是一个不同的问题!

#6


2  

I use elements for data and attributes for Metadata

我使用元素作为数据,使用属性作为元数据

#7


1  

I prefer the second. Elements should be used to describe data (which is mostly what you are doing). Attributes are used for values not relevant to the data, such as the max size for an element for example.

我还是更喜欢第二份文件。元素应该用于描述数据(这主要是您正在做的)。属性用于与数据无关的值,例如元素的最大大小。

#8


1  

IBM has posted an article titled Principles of XML Design that provides some guidelines on when to use attributes vs. elements. I found this article useful, your mileage may vary.

IBM发表了一篇题为《XML设计原则》的文章,该文章提供了关于何时使用属性和元素的一些指导原则。我发现这篇文章很有用,你的里数可能不同。

#9


1  

The first alternative scales a bit better. Suppose that you need to add another attribute of an animal section, such as the status of the section. I suggest that this representation:

第一种选择的规模稍好一些。假设您需要添加一个动物部分的另一个属性,例如section的状态。我建议这一表示:

<sitemap>
  <category name="Animals">
    <section title="Dogs" status="draft">
      ...
    </section>
  </category>
</sitemap>

does a better job of conveying the following facts:

更好地传达以下事实:

  1. name is a property of the category
  2. 名称是类别的属性
  3. a category could have multiple sections
  4. 一个类别可以有多个部分
  5. status is a property of a section; not all sections in the category are required to have the same status.
  6. 状态是一节的属性;类别中的并非所有部分都要求具有相同的状态。

In short, it makes the hierarchical structure clearer, and shows which attributes apply at each level of the hierarchy.

简而言之,它使层次结构更清晰,并显示在层次结构的每个层次上应用哪些属性。

#10


1  

A simple rule of thumb: if you can implement a data structure as an unordered map of name/value pairs, you can use an element's attributes to represent it. If you can't (if, for instance, you'll have multiple names, or a given name will have multiple associated value, or the ordering of the name/value pairs is significant), then an element with attributes is the wrong representation.

一个简单的经验法则是:如果可以将数据结构实现为一个无序的名称/值对映射,那么可以使用元素的属性来表示它。如果不能(例如,如果有多个名称,或者给定的名称有多个关联值,或者名称/值对的排序很重要),那么具有属性的元素就是错误的表示。

Two other things that can make this the wrong representation:

另外两件事可能会让这个表述错误:

  • The values contain markup. This can be represented in attribute values, but it's awkward, because all of the markup characters have to be escaped into entities. Also, the markup won't be parsed.
  • 包含标记的值。这可以用属性值表示,但这很尴尬,因为所有的标记字符都必须转义到实体中。此外,不会解析标记。
  • You're using XML Schema validation, and there is more than one allowable set of name/value pairs. XML Schema can only define one set of allowable attributes for an element, whereas it can define multiple mutually-exclusive sets of allowable child elements.
  • 您正在使用XML模式验证,并且有不止一组允许的名称/值对。XML Schema只能为一个元素定义一组允许的属性,而它可以定义多个允许的子元素的互斥集合。

The obvious benefit to using attributes is that they result in terser XML. They're (very) marginally faster to parse than elements, too.

使用属性的明显好处是它们可以生成更简洁的XML。它们比元素的解析速度要快得多。

#1


20  

The issue of attributes vs elements has been around for the better part of a decade and there is no right answer. Instead consider the differences and from that you should be able to decide which to use:

关于属性vs元素的问题已经存在了近十年,而且没有正确的答案。相反,你应该考虑不同之处,从中你应该能够决定使用哪一种:

  • There can be only one instance of an attribute although you can enforce this with elements using DTD or XML Schema;
  • 虽然可以使用DTD或XML模式对元素进行强制执行,但也只能有一个属性实例;
  • Attributes are unordered. Elements are not;
  • 属性是无序的。元素就不明智了;
  • Attributes lead to a more concise syntax if there are no children. Compare:

    如果没有子属性,属性将导致更简洁的语法。比较:

    <page name="Sitemap"/>

    页面< name = "地图" / >

to:

:

<page>
  <name>Sitemap</name>
</page>

I know which one I prefer;

我知道我更喜欢哪个;

  • Not really relevant now since DTDs aren't used much (if at all) over XML Schema but I'll add it anyway: DTDs allow default values (implied) for attributes but no such mechanism for elements; and
  • 由于dtd没有在XML模式中大量使用(如果有的话),所以现在并不是很相关,但是我还是要添加它:dtd允许属性的默认值(隐含值),但是对于元素没有这样的机制;和
  • Elements, being elementss, can have children and attributes of their own. Attributes obviously cannot.
  • 元素作为元素,可以有自己的子元素和属性。属性显然不能。

So, from your example, your innermost <page> element has a URL attribute (although it's an image for some reason--perhaps a preview icon? If so the attribute name is misleading). A webpage only has one URL (generally) so that'd be a good example of something that could be an attribute.

因此,从您的示例中,最内层的 元素有一个URL属性(尽管出于某种原因它是一个图像——也许是一个预览图标?)如果是这样,属性名就具有误导性)。一个网页(通常)只有一个URL,这是一个很好的例子,可以是一个属性。

If on the other hand you wanted to list the images on the page, there could obviously be more than one so you'd need elements for that.

另一方面,如果你想要在页面上列出图片,显然会有不止一个,所以你需要一些元素。

But, in the end, most of the time there's no right or wrong answer and it's largely a question of style.

但是,最后,大多数时候没有正确或错误的答案,这很大程度上是一个风格问题。

#2


12  

The two examples are not equivalent, because they form different hierarchies. Is a sitemap a list of categories, like the first example? Or is it a list of pages like the second example?

这两个例子并不相同,因为它们形成了不同的层次结构。站点地图是一个类别列表吗?或者是像第二个例子那样的页面列表?

The answer to that is orthogonal to the element vs attribute question.

这个问题的答案与元素vs属性问题是正交的。

On the Element vs Attribute question: Here is your second example transformed to an attribute approach:

关于元素vs属性的问题:这是您将第二个示例转换为属性方法的例子:

<sitemap>
 <page    
  category='Animals'
  section='Dogs'
  title='Great Dane'
  url='/pics/greatdane.jpg'
  /> 
</sitemap>

The above and your second case are equivalent. One consideration for choosing one versus the other is based on whether you may modify the schema in the future. Adding an attribute to the url element as in the following example would likely be a backward compatable change. The semantically same modification would be impossible in the attribute approach, as you cannot attach an attribute to an attribute.

以上两种情况是等价的。选择一个与另一个的考虑是基于您是否可以在将来修改模式。向url元素添加属性(如下例所示)可能是一种向后可压缩的更改。在属性方法中,语义相同的修改是不可能的,因为您不能将属性附加到属性。

<sitemap>
 <page>    
  <category>Animals</category>
  <section>Dogs</section>    
  <title>Great Dane</title>    
  <url nofollow="true">/pics/greatdane.jpg</url>
 </page> 
</sitemap>

#3


3  

I think that the answer is quite obvious when you think about how you want to add more dogs:

我认为答案是很明显的当你想要增加更多的狗:

<sitemap>
  <category name="Animals">
    <section title="Dogs">
      <page url="/pics/greatdane.jpg" title="Great Dane"/>
      <page url="/pics/wienerdog.jpg" title="Wiener Dog"/>
    </section>
  </category>
</sitemap>

or

<sitemap>
  <page>
    <category>Animals</category>
    <section>Dogs</section>
    <title>Great Dane</title>
    <url>/pics/greatdane.jpg</url>    
  </page>
  <page>
    <category>Animals</category>
    <section>Dogs</section>
    <title>Wiener Dog</title>
    <url>/pics/wienerdog.jpg</url>
  </page>
</sitemap>

#4


2  

There aren't usually right or wrong answers for this sort of thing. Mostly it depends on how you need to access your data.

这类事情通常没有正确或错误的答案。这主要取决于您如何访问您的数据。

One nice thing about the first one is that it easily, perhaps later, supports multiple pages in a section and multiple sections in a category. In the second one that information is spread across pages.

第一个优点是,它很容易在一个类别中支持多个页面和多个部分。在第二种方法中,信息被分散在不同的页面上。

#5


2  

XML is horrible file format and ends up in religious wars. Do whatever you feel is best at the time, provided you can justify it. However, your particular examples are significantly different:

XML是一种糟糕的文件格式,最终导致宗教战争。只要你能证明它的合理性,就做你认为最好的事情。然而,你的具体例子有很大的不同:

In 1, sitemap encapsulates a category encapsulating a section which encapsulates the page.

在1中,sitemap封装了封装页面的部分的类别。

In 2, sitemap encapsulates a page which encapsulates three items: category, section, title and URL. None of these three items contains any other, they are siblings, all commonly held within the page.

在2中,sitemap封装了一个页面,该页面封装了三个条目:类别、部分、标题和URL。这三个项目中没有一个包含其他的,它们都是同级的,都是页面中常见的。

As two different structures, it depends on which you intended.

作为两个不同的结构,它取决于你想要哪个。

A different question would be the preference for attributes versus first-level tags. But as I say, that is a different question!

另一个问题是对属性和一级标签的偏好。但正如我所说,这是一个不同的问题!

#6


2  

I use elements for data and attributes for Metadata

我使用元素作为数据,使用属性作为元数据

#7


1  

I prefer the second. Elements should be used to describe data (which is mostly what you are doing). Attributes are used for values not relevant to the data, such as the max size for an element for example.

我还是更喜欢第二份文件。元素应该用于描述数据(这主要是您正在做的)。属性用于与数据无关的值,例如元素的最大大小。

#8


1  

IBM has posted an article titled Principles of XML Design that provides some guidelines on when to use attributes vs. elements. I found this article useful, your mileage may vary.

IBM发表了一篇题为《XML设计原则》的文章,该文章提供了关于何时使用属性和元素的一些指导原则。我发现这篇文章很有用,你的里数可能不同。

#9


1  

The first alternative scales a bit better. Suppose that you need to add another attribute of an animal section, such as the status of the section. I suggest that this representation:

第一种选择的规模稍好一些。假设您需要添加一个动物部分的另一个属性,例如section的状态。我建议这一表示:

<sitemap>
  <category name="Animals">
    <section title="Dogs" status="draft">
      ...
    </section>
  </category>
</sitemap>

does a better job of conveying the following facts:

更好地传达以下事实:

  1. name is a property of the category
  2. 名称是类别的属性
  3. a category could have multiple sections
  4. 一个类别可以有多个部分
  5. status is a property of a section; not all sections in the category are required to have the same status.
  6. 状态是一节的属性;类别中的并非所有部分都要求具有相同的状态。

In short, it makes the hierarchical structure clearer, and shows which attributes apply at each level of the hierarchy.

简而言之,它使层次结构更清晰,并显示在层次结构的每个层次上应用哪些属性。

#10


1  

A simple rule of thumb: if you can implement a data structure as an unordered map of name/value pairs, you can use an element's attributes to represent it. If you can't (if, for instance, you'll have multiple names, or a given name will have multiple associated value, or the ordering of the name/value pairs is significant), then an element with attributes is the wrong representation.

一个简单的经验法则是:如果可以将数据结构实现为一个无序的名称/值对映射,那么可以使用元素的属性来表示它。如果不能(例如,如果有多个名称,或者给定的名称有多个关联值,或者名称/值对的排序很重要),那么具有属性的元素就是错误的表示。

Two other things that can make this the wrong representation:

另外两件事可能会让这个表述错误:

  • The values contain markup. This can be represented in attribute values, but it's awkward, because all of the markup characters have to be escaped into entities. Also, the markup won't be parsed.
  • 包含标记的值。这可以用属性值表示,但这很尴尬,因为所有的标记字符都必须转义到实体中。此外,不会解析标记。
  • You're using XML Schema validation, and there is more than one allowable set of name/value pairs. XML Schema can only define one set of allowable attributes for an element, whereas it can define multiple mutually-exclusive sets of allowable child elements.
  • 您正在使用XML模式验证,并且有不止一组允许的名称/值对。XML Schema只能为一个元素定义一组允许的属性,而它可以定义多个允许的子元素的互斥集合。

The obvious benefit to using attributes is that they result in terser XML. They're (very) marginally faster to parse than elements, too.

使用属性的明显好处是它们可以生成更简洁的XML。它们比元素的解析速度要快得多。