我正在为我当前的用例设计 XML 文档。我的用例是-
给定一个 id,我可以获得检查分数、天赋分数和类别数量。每个类别都会有类别ID、检查分数和天赋分数。
所以假设如果我有两个 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
以及与之相关的相应事物id
。意思是,我如何表示第二个 id 以及与上面示例中相关的其他组件?如果我有两个 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>
<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 的正确方法是什么。
谁可以帮我这个事?
我遵循 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 几乎总是错误的正在描述一个方面一些信息(阅读:元素)。
当然,也有可能是这样的情况scores是容器,类别(id)只是一个的方面这种分数信息(元素)(的属性)。
无论如何,我强烈建议使用容器来实现无限制的多重性。它让生活更轻松。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)