如何用XML表示列表数据 [英] How to represent list data in XML
问题描述
我正在为我的当前用例设计XML文档.我的用例是-
I am working on designing the XML document for my current use case. My use case is-
给出一个ID,我可以得到检查分数和才能分数以及类别数.每个类别都有类别编号,检查分数和人才分数.
Given an id, I can have check-score and talent-score and number of categories. And each category will have category-id, check-score and talent-score.
因此,假设我有两个ID,则上面的数据将存在于两个ID中.
So suppose if I have two id's, then the above data will be there in two id's.
例如,下面是我基于单个id创建的XML-
For example, below is my XML that I have created basis on single id-
<?xml version="1.0" encoding="UTF-8" ?>
<id>0</site-id>
<check-score>0.5</check-score>
<talent-score>0.2</talent-score>
<categories>
<category-id>123</category-id>
<check-score>0.5</check-score>
<talent-score>0.2</talent-score>
</categories>
<categories>
<category-id>321</category-id>
<check-score>0.2</check-score>
<talent-score>0.4</talent-score>
</categories>
如果我有另一个值为 1
的 id
以及与该 id
相关的相应内容,将会发生什么.意思是,在上面的示例中,我该如何表示第二个ID和与之相关的其他组件?如果我有两个ID,我应该做这样的事情吗?
What will happen if I have another id
with value as 1
and corresponding things related to that id
. Meaning, how do I represent second id and other components related to that in my above example? Should I do something like this if I have two id's?
<?xml version="1.0" encoding="UTF-8" ?>
<id>0</site-id>
<check-score>0.5</check-score>
<talent-score>0.2</talent-score>
<categories>
<category-id>123</category-id>
<check-score>0.5</check-score>
<talent-score>0.2</talent-score>
</categories>
<categories>
<category-id>321</category-id>
<check-score>0.2</check-score>
<talent-score>0.4</talent-score>
</categories>
<id>1</site-id>
<check-score>0.2</check-score>
<talent-score>0.3</talent-score>
<categories>
<category-id>289</category-id>
<check-score>0.3</check-score>
<talent-score>0.7</talent-score>
</categories>
<categories>
<category-id>987</category-id>
<check-score>0.1</check-score>
<talent-score>0.5</talent-score>
</categories>
也许有可能,我可以有多个id,所以我不确定为上述用例编写XML的正确方法.
And it might be possible, I can have multiple id's so I am not sure what is the right way to write an XML for my above use case.
有人可以帮我吗?
推荐答案
我遵循XML中的将容器用于未绑定的多重性"规则.
I follow the rule of "use containers for unbound multplicity" in XML.
这通常更易于使用,更易于在模式中表达,并且全方位一致且易于处理和扩展.此外,使用自动序列化/类型映射器可以很好地支持使用这样的方法.如上面的第二个示例所示,为此类结构化数据创建混合模式标记将导致混乱,并且需要进行特殊处理.
This is generally easier to consume, cleaner to express in schemas, and all-around consistent and simple to deal with and extend. In addition, using an approach like this is well supported using automatic serialization/type mappers. As shown above in the 2nd example, creating mixed mode markup for such structured data will result in a mess and requires special handling to deal with.
例如,将容器用于多重性:
For example, to use containers for the multiplicity:
<someRelevantRootElement>
<sites>
<site site-id="0">
<!-- not sure what scores are doing there -->
<categories>
<category category-id="123">
<scores check="0.5" talent="0.2" />
</category>
<category category-id="..">
<!-- .. -->
</category>
<!-- more categories? -->
</categories>
</site>
<site side-id="..">
<!-- .. -->
</site>
<!-- more sites? -->
</sites>
</someRelevantRootElement>
还请注意,我将某些元素转换为属性.将元素用作"id"几乎总是错误的,因为id 描述了某些信息的一个方面(阅读:element).
Also note that I converted some elements to attributes. Using an element for an "id" is almost always wrong as an id is describing an aspect of some information (read: element).
当然,可能是得分是容器,并且类别(id)只是此类得分信息的方面(的一个属性)(元素).
Of course, it could be the case that scores are the container, and the category (id) is just an aspect of (an attribute of) such score information (element).
无论如何,我敦促使用容器的无限制多重性.它使生活更轻松.
In any case, I urge the use of containers-for-unbound-multiplicity. It makes life easier.
这篇关于如何用XML表示列表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!