你应该如何构建你的 xml 文件? [英] How should you structure your xml file?

查看:36
本文介绍了你应该如何构建你的 xml 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建新的 xml 文件时,如何正确或以最佳方式构建文件.通过结构,在这种情况下这可能不是最好的词,我的意思是如何在将某物作为元素或元素的属性之间进行选择.例如,如果我创建一个包含人员列表的 Person.xml 文件,最好执行以下操作:

When creating a new xml file, how does one go about structuring the file correctly or the best possible way. By structure, which may not be the best word in this case, I mean how does one choose between making something an element or an attribute of an element. For example, if I create a Person.xml file which contains a list of Persons, is it better to do something like:

<Person>
    <FirstName>John</FirstName>
    <LastName>Doe</LastName>
    <Age>23</Age>
</Person>

或者做这样的事情更好还是重要?

or is it better to do something like this or does it even matter?

<Person FirstName="John" LastName="Doe" Age="23"></Person>

推荐答案

XML 文件的结构应该(不要引发一场圣战)如下:

XML files should (not to start a holy war) be structured as follows:

如果是数据,或者可以改变的东西,那么应该是这样的:

If it's data, or something that can be changed, then it should be like this:

<Person>
  <FirstName>John</FirstName>
  <LastName>Smith</LastName>
  <Age>23</Age>
</Person>

如果它是事物Person的一个属性,那么它应该是这样的:

If it's an attribute of the thing Person then it should be like this:

<Person Type="Human">
  <FirstName>John</FirstName>
  <LastName>Smith</LastName>
  <Age>23</Age>
</Person>

这种做法有多种原因,其中最重要的原因包括在您更改检索人员数据的方法时轻松修复 XSLT 转换.

There are multiple reasons for this practice, not the least of which includes the ease of fixing your XSLT Transforms whenever you change your method of retrieving Person data.

这确实是重要的部分:属性定义了有关数据的信息(人员类型),而数据旨在填补这些漏洞.如果您决定如何改变填补这些漏洞的方式,那么当您以后想要转换 XML 时,如果您将它们设为属性"而不是数据"就会变得更加困难.

That's really the important part: Attributes define information about data (the Person Type), and the Data is something that is meant to fill in those holes. If you decide how you're going to change how you fill in those holes, then it becomes tougher if you've made them 'attributes' instead of 'data' when you want to Transform your XML later.

这篇关于你应该如何构建你的 xml 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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