XML 属性与 XML 元素 [英] XML attribute vs XML element

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

问题描述

在工作中,我们被要求创建 XML 文件以将数据传递到另一个离线应用程序,然后该应用程序将创建第二个 XML 文件以传回以更新我们的某些数据.在此过程中,我们一直在与其他应用程序的团队讨论 XML 文件的结构.

At work we are being asked to create XML files to pass data to another offline application that will then create a second XML file to pass back in order to update some of our data. During the process we have been discussing with the team of the other application about the structure of the XML file.

我想出的样本本质上是这样的:

The sample I came up with is essentially something like:

<INVENTORY>
   <ITEM serialNumber="something" location="something" barcode="something">
      <TYPE modelNumber="something" vendor="something"/> 
   </ITEM>
</INVENTORY>

另一个团队说这不是行业标准,属性应该只用于元数据.他们建议:

The other team said that this was not industry standard and that attributes should only be used for meta data. They suggested:

<INVENTORY>
   <ITEM>
      <SERIALNUMBER>something</SERIALNUMBER>
      <LOCATION>something</LOCATION>
      <BARCODE>something</BARCODE>
      <TYPE>
         <MODELNUMBER>something</MODELNUMBER>
         <VENDOR>something</VENDOR>
      </TYPE>
   </ITEM>
</INVENTORY>

我建议第一个的原因是创建的文件的大小要小得多.在传输过程中,文件中将有大约 80000 个项目.他们的建议实际上比我建议的要大三倍.我搜索了提到的神秘的行业标准",但我能找到的最接近的是 XML 属性应该只用于元数据,但说争论是关于什么是真正的元数据.

The reason I suggested the first is that the size of the file created is much smaller. There will be roughly 80000 items that will be in the file during transfer. Their suggestion in reality turns out to be three times larger than the one I suggested. I searched for the mysterious "Industry Standard" that was mentioned, but the closest I could find was that XML attributes should only be used for meta data, but said the debate was about what was actually meta data.

经过冗长的解释(抱歉)之后,您如何确定元数据是什么?在设计 XML 文档的结构时,您应该如何决定何时使用属性或元素?

After the long winded explanation (sorry) how do you determine what is meta data, and when designing the structure of an XML document how should you decide when to use an attribute or an element?

推荐答案

我使用这个经验法则:

  1. 属性是自包含的东西,即颜色、ID、名称.
  2. 元素是具有或可能具有自己的属性或包含其他元素的事物.

所以你的很接近.我会做类似的事情:

So yours is close. I would have done something like:

编辑:根据以下反馈更新了原始示例.

EDIT: Updated the original example based on feedback below.

  <ITEM serialNumber="something">
      <BARCODE encoding="Code39">something</BARCODE>
      <LOCATION>XYX</LOCATION>
      <TYPE modelNumber="something">
         <VENDOR>YYZ</VENDOR>
      </TYPE>
   </ITEM>

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

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