Reputation:
I am working on designing the XML document for my current use case. My use case is-
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.
So suppose if I have two id's, then the above data will be there in two id's.
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>
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>
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.
Can anyone help me on this?
Upvotes: 6
Views: 24933
Reputation: 7719
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>
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).
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.
Upvotes: 2