使用父 XML 元素作为重复出现子元素的容器? [英] Use a parent XML element as a container for reoccurring child elements?

查看:19
本文介绍了使用父 XML 元素作为重复出现子元素的容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您希望某些特定的 XML 元素出现 0 次以上.例如,<record> 元素可以出现多次:

Let's say you want to allow some particular XML element to occur 0+ times. For example, the <record> element can occur multiple times:

<repository>
  <record>Record 1</record>
  <record>Record 2</record>
  <record>Record 3</record>
</repository>

是否有任何令人信服的理由将父元素作为容器包含在内?例如,下面使用 <recordSet> 元素来包含 <record> 元素:

Are there any compelling reasons to include a parent element as a container for these? For example, the following uses the <recordSet> element to contain the <record> elements:

<repository>
  <recordSet>
    <record>Record 1</record>
    <record>Record 2</record>
    <record>Record 3</record>
  </recordSet>
</repository>

在提出此问题之前,我试图找到任何相关问题,因此如果已经被问到,我深表歉意.提前感谢您的所有意见!

I tried to find any related questions before posing this, so my apologies if it has been asked already. Thanks in advance for any and all input!

编辑 - 2009 年 7 月 22 日:

Edit - July 22, 2009:

感谢大家的出色反馈!(我会评论/投票给人们,但我还没有足够的声望点.)我可能会继续这条路线,我还要感谢@16bytes 的建议,包括简单地命名父母通过使用重复出现的子元素的复数形式.

Thanks to everyone for the excellent feedback! (I would comment / vote people up, but I don't have enough reputation points quite yet.) I'll probably go ahead with this route, and I also wanted to thank @16bytes for their advice, including to simply name the parent by using the plural of the reoccurring child elements.

推荐答案

拥有父元素可以更轻松地识别 XML 的各个部分,并使处理程序更容易将子元素映射到集合.如果您有一个禁止使用属性的系统(有些这样做,这很烦人),您还需要使用包装器元素来区分属于特定子项的属性(例如 id).

Having a parent element makes it simpler to identify sections of the XML and makes it simpler for handlers to map the child elements to a collection. If you have a system that forbids the use of attributes (some do, it's annoying), you also need to use wrapper elements to distinguish properties (such as ids) that belong to particular children.

除此之外,省略父元素也是有效的,并且可以使文件明显不那么冗长.

Other than that it is valid to omit the parent element and can make the file markedly less verbose.

在您的第一个示例中,用户元素可能与记录混合在一起,这是有效的,但可能很难发现:

In your first example, a user element could be mingled in with the records, this is valid but it might be hard to spot:

<repository>
  <record>Record 1</record>
  <record>Record 2</record>
  <user>Bill</user>
  <record>Record 3</record>
</repository>

而使用周围元素,您可以分隔集合,并拥有该集合的多个实例:

Whereas with a surrounding element you can separate the collection, and have multiple instances of that collection:

<repositories>
  <users>
    <user>Bill</user>
  </users>
  <repository>
    <id>id1</id>
    <recordSet>
      <id>recordSet1</id>
      <record>Record 1</record>
      <record>Record 2</record>
      <record>Record 3</record>
    </recordSet>
    <recordSet>
      <id>recordSet2</id>
      <record>Record 1</record>
      <record>Record 2</record>
      <record>Record 3</record>
    </recordSet>
  </repository>
  <repository>
    <id>id2</id>
    <recordSet>
      <record>Record 1</record>
      <record>Record 2</record>
      <record>Record 3</record>
    </recordSet>
  </repository>
</repositories>

这篇关于使用父 XML 元素作为重复出现子元素的容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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