XML 元素是否应该有分组父元素?

发布于 2024-08-15 16:33:58 字数 524 浏览 5 评论 0原文

首选哪种约定以及为什么(包括一种约定相对于另一种约定的一些优缺点)?

这个:

<company>
    <employees>
        <employee />
        <employee />
        <employee />
    </employees>
    <buildings>
        <building />
        <building />
    </building>
</company>

或者这个:

<company>
    <employee />
    <employee />
    <employee />
    <building />
    <building />
</company>

Which convention would be preferred and why (include some pros and cons of one over the other)?

This:

<company>
    <employees>
        <employee />
        <employee />
        <employee />
    </employees>
    <buildings>
        <building />
        <building />
    </building>
</company>

or this:

<company>
    <employee />
    <employee />
    <employee />
    <building />
    <building />
</company>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

七禾 2024-08-22 16:33:58

通常,在编程中处理 XML 时,您经常希望将其转换为程序可以使用的对象。大多数 XML 序列化程序使用元素来表示父元素的属性。

当您像上面的示例一样对它们进行分组时,序列化程序可以将其解释为单个集合或数组属性。在本例中,您将拥有一个 Company,它具有一个 Employees 属性,该属性是 Employee 对象的集合。

如果您采用第二种方式,您最终会得到一个 Company 对象,其属性名为“Employee”、“Employee2”、“ Employee3”或类似的东西。当你编程时,这不是一个好的对象设计。

Generally when dealing with XML in programming, you often want to translate it to objects that your program can use. Most XML serializers take elements to represent properties of the parent element.

When you group them like in your top example, serializers can interpret that as a single collection or array property. In this case, you'd have a Company, which has an Employees property, which is a collection of Employee objects.

If you do it the second way, you'll end up with a Company object with properties called "Employee", "Employee2", "Employee3" or something similar. When you're programming, that's not a good object design.

恰似旧人归 2024-08-22 16:33:58

通过特定元素来显式表示集合没有任何语义原因。最后,两份文件具有相同的含义——它们代表一家公司及其所有员工和建筑物。

然而,第一个示例还有其他好处:

  • 它更具可读性,并且可以从编辑器中的大纲中受益。
  • 您可以创建更严格的模式
  • 更容易序列化为强类型集合
  • 您可以在集合元素上指定适用于内部所有元素的属性

另一方面,第二个示例也有其自己的一些好处(尽管我确实发现这些可疑至少):

  • 更少啰嗦/使用更少的内存
  • 更容易通过非 XML 工具进行处理

There are no semantic reasons to have explicit representation of the collection through a specific element. In the end, both documents have the same meaning - they represent a company with all of its employees and buildings.

However, there are additional benefits the first example offers:

  • it is more readable and can benefit from outlining in editors.
  • you can create more strict schema
  • it's easier to serialize to strongly typed collections
  • you can specify attributes on the collection element that apply to all elements inside

On the flip side, the second example also has some benefits of its own (though I do find these dubious at least):

  • is less chatty/uses less memory
  • is easier to process through non-XML tools
GRAY°灰色天空 2024-08-22 16:33:58

是否有可以附加到组中所有员工的属性或数据?我通常会选择第一个示例(分组元素),因为它使您可以自由地

  1. 创建多个组
  2. ,将数据作为一个整体附加到组

它有点冗长,但给您带来了很多好处未来更加灵活。

Are there attributes or data you could possibly attach to all employees in a group ? I'd normally go for the first example (a grouping element) since it gives you the freedom to

  1. create multiple groups
  2. attach data to the group as a whole

It's a little more verbose, but buys you a lot more flexibility going forwards.

丶情人眼里出诗心の 2024-08-22 16:33:58

用一个问题来回答你的问题...分组元素如何影响视觉上和编程上的可读性?

分组方法(使用 )更加直观。

对于 XPath 查询来说,非分组方法更加简洁,/company/employees/employee/company/employee 相比。

因此,其中一个可能有 6 个,另一个可能有 6 个。

To answer your question with a question... How do grouping elements affect readability, both visually and programmatically?

The grouped method (with the <employees> and <buildings>) is more visual.

The non-grouped method is a little more concise for an XPath query, /company/employees/employee vs. /company/employee.

So, it may be 6 of one, half a dozen of the other.

瑾兮 2024-08-22 16:33:58

我会通过思考你所代表的数据来解决这个问题。

在 C# 中,我看到了这一点:

// Top Example:
public class Company
{
  public Employee[] employees;
  public Building[] buildings;
}

// Vs. Bottom Example:
public Things[] employeesAndBuildings;

从逻辑上讲,建筑物和员工不是同一件事。由于您的公司拥有多栋大楼和多名员工,因此您应该从逻辑上将这两个组分开。

第二个示例较短,根据阅读格式的内容,读者可以轻松地将这两个项目分开。但为了可读性,当事情以不同的顺序进行时,格式可能会变得更加难以阅读:

<company>
    <employee />
    <building />
    <employee />
    <building />
    <employee />
</company>

我会选择第一种格式。

I'd approach it by thinking of what data you're representing.

In C#, I see this:

// Top Example:
public class Company
{
  public Employee[] employees;
  public Building[] buildings;
}

// Vs. Bottom Example:
public Things[] employeesAndBuildings;

Logically, buildings and employees are not the same thing. Since your company has several buildings and several employees, you should logically seperate the two groups.

The second example is shorter and depending on what is reading the format in, the reader can just as easily separate the two items out. For readability though, the format can get more unreadable when things go in a different order:

<company>
    <employee />
    <building />
    <employee />
    <building />
    <employee />
</company>

I'd go for the first format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文