XML属性与元素 [英] XML Attributes vs Elements

查看:91
本文介绍了XML属性与元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时应使用XML属性以及何时应使用XML元素?

When should you use XML attributes and when should you use XML elements?

例如

<customData>
  <records>
    <record name="foo" description="bar" />
  </records>
</customData>

<customData>
  <records>
    <record>
      <name>foo</name>
      <description>bar</description>
    </record>
  </records>
</customData>

推荐答案

有一篇文章标题为" XML设计原则:何时在IBM网站上使用元素与属性.

尽管似乎没有很多严格的规则,但在帖子中提到了一些好的指导原则.例如,建议之一是当您的数据不能针对空白进行规范化时使用元素,因为XML处理器可以规范属性内的数据从而修改原始文本.

Though there doesn't appear to be many hard and fast rules, there are some good guidelines mentioned in the posting. For instance, one of the recommendations is to use elements when your data must not be normalized for white space as XML processors can normalize data within an attribute thereby modifying the raw text.

在开发各种XML结构时,我会不时提及本文.希望这对其他人也有帮助.

I find myself referring to this article from time to time as I develop various XML structures. Hopefully this will be helpful to others as well.

编辑-从站点:

核心内容原则

如果您认为有问题的信息是用XML表示或传达的基本材料的一部分,请将其放在一个元素中.对于人类可读的文档,这通常意味着正在传达给读者的核心内容.对于面向机器的记录格式,这通常意味着直接来自问题域的数据.如果您认为该信息是主要通信的附带或附带信息,或者纯粹是旨在帮助应用程序处理主要通信的信息,请使用属性.这避免了用辅助材料使堆芯内容物杂乱无章.对于面向机器的记录格式,这通常意味着对来自问题域的主要数据使用特定于应用程序的符号.

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. For human-readable documents this generally means the core content that is being communicated to the reader. For machine-oriented records formats this generally means the data that comes directly from the problem domain. 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. This avoids cluttering up the core content with auxiliary material. For machine-oriented records formats, this generally means application-specific notations on the main data from the problem-domain.

作为一个例子,我看到了许多XML格式,通常在企业中自行开发,将文档标题放置在一个属性中.我认为标题是文档沟通的基础部分,因此标题应始终位于元素内容中.另一方面,我经常看到这样的情况:将内部产品标识符作为元素扔到产品的描述性记录中.在某些情况下,属性更合适,因为特定的内部产品代码对于文档的大多数阅读者或处理者而言并不是最重要的,尤其是当ID的格式非常长或难以理解时.

As an example, I have seen many XML formats, usually home-grown in businesses, where document titles were placed in an attribute. I think a title is such a fundamental part of the communication of a document that it should always be in element content. On the other hand, I have often seen cases where internal product identifiers were thrown as elements into descriptive records of the product. In some of these cases, attributes were more appropriate because the specific internal product code would not be of primary interest to most readers or processors of the document, especially when the ID was of a very long or inscrutable format.

您可能已经听说过原理数据放在元素中,元数据放在属性中.上面两段确实表达了相同的原理,但是用的是故意的和模糊的语言.

You might have heard the principle data goes in elements, metadata in attributes. The above two paragraphs really express the same principle, but in more deliberate and less fuzzy language.

结构化信息原理

如果信息以结构化形式表示,特别是如果结构可能是可扩展的,则使用元素.另一方面:如果信息表示为原子标记,请使用属性.元素是用于表达XML结构的可扩展引擎.几乎所有XML处理工具都是围绕这一事实而设计的,如果您将结构化信息正确地分解为元素,您会发现处理工具对您的设计起到了补充作用,从而提高了生产率和可维护性.设计属性是为了表达元素中表示的信息的简单属性.如果您通过将结构化信息塞入属性而违反XML的基本体系结构,则可能会获得一些简洁性和便利性,但您可能会付出维护费用.

If the information is expressed in a structured form, especially if the structure may be extensible, use elements. On the other hand: If the information is expressed as an atomic token, use attributes. Elements are the extensible engine for expressing structure in XML. Almost all XML processing tools are designed around this fact, and if you break down structured information properly into elements, you'll find that your processing tools complement your design, and that you thereby gain productivity and maintainability. Attributes are designed for expressing simple properties of the information represented in an element. If you work against the basic architecture of XML by shoehorning structured information into attributes you may gain some specious terseness and convenience, but you will probably pay in maintenance costs.

日期是一个很好的例子:日期具有固定的结构,通常充当单个标记,因此它作为属性是有意义的(最好用ISO-8601表示).另一方面,在我看到这种原理使设计师感到惊讶的情况下,代表个人名字.我在属性中经常看到名字,但是我一直认为个人名字应该包含在元素内容中.一个人名的结构令人惊讶地可变(在某些文化中,您可能通过省略敬语或假设姓名的顺序而引起混乱或冒犯).个人名称也很少是原子标记.例如,有时您可能想按姓氏,有时按姓氏进行搜索或排序.我应该指出,将一个全名插入单个元素的内容和将其放入属性一样是有问题的.

Dates are a good example: A date has fixed structure and generally acts as a single token, so it makes sense as an attribute (preferably expressed in ISO-8601). Representing personal names, on the other hand, is a case where I've seen this principle surprise designers. I see names in attributes a lot, but I have always argued that personal names should be in element content. A personal name has surprisingly variable structure (in some cultures you can cause confusion or offense by omitting honorifics or assuming an order of parts of names). A personal name is also rarely an atomic token. As an example, sometimes you may want to search or sort by a forename and sometimes by a surname. I should point out that it is just as problematic to shoehorn a full name into the content of a single element as it is to put it in an attribute.

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

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