XML 元素是否应该有分组父元素?
首选哪种约定以及为什么(包括一种约定相对于另一种约定的一些优缺点)?
这个:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通常,在编程中处理 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 anEmployees
property, which is a collection ofEmployee
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.通过特定元素来显式表示集合没有任何语义原因。最后,两份文件具有相同的含义——它们代表一家公司及其所有员工和建筑物。
然而,第一个示例还有其他好处:
另一方面,第二个示例也有其自己的一些好处(尽管我确实发现这些可疑至少):
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:
On the flip side, the second example also has some benefits of its own (though I do find these dubious at least):
是否有可以附加到组中所有员工的属性或数据?我通常会选择第一个示例(分组元素),因为它使您可以自由地
它有点冗长,但给您带来了很多好处未来更加灵活。
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
It's a little more verbose, but buys you a lot more flexibility going forwards.
用一个问题来回答你的问题...分组元素如何影响视觉上和编程上的可读性?
分组方法(使用
和
)更加直观。对于 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.
我会通过思考你所代表的数据来解决这个问题。
在 C# 中,我看到了这一点:
从逻辑上讲,建筑物和员工不是同一件事。由于您的公司拥有多栋大楼和多名员工,因此您应该从逻辑上将这两个组分开。
第二个示例较短,根据阅读格式的内容,读者可以轻松地将这两个项目分开。但为了可读性,当事情以不同的顺序进行时,格式可能会变得更加难以阅读:
我会选择第一种格式。
I'd approach it by thinking of what data you're representing.
In C#, I see this:
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:
I'd go for the first format.