我正在为当前的用例设计XML文档。我的用例是 -
鉴于身份证,我可以获得支票得分和天赋得分以及类别数量。每个类别都有category-id,check-score和talent-score。
假设我有两个id,那么上面的数据就会出现在两个id中。
例如,下面是我在单个id上创建的XML -
<?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}}以及与该1
相关的相应内容,会发生什么情况。意思是,我如何在上面的例子中表示与之相关的第二个id和其他组件?如果我有两个id,我应该这样做吗?
id
有可能,我可以拥有多个id,所以我不确定为上述用例编写XML的正确方法是什么。
有人可以帮我吗?
答案 0 :(得分:0)
我遵循XML中“使用容器进行无绑定多重性”的规则。
这通常更容易使用,在模式中表达更清晰,并且全面一致且易于处理和扩展。此外,使用自动序列化/类型映射器可以很好地支持使用这样的方法。如上面第二个例子所示,为这样的结构化数据创建混合模式标记将导致混乱并需要特殊处理来处理。
例如,要使用容器来实现多重性:
<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 描述了某些信息的一个方面(读取:元素)。
当然,可能是得分是容器的情况,而类别(id)只是 (这是一种属性)这样的得分信息的方面(元件)。
无论如何,我敦促使用容器 - 未绑定 - 多重性。它让生活更轻松。