Converting a one-to-many xml design to a many-to-many
https://softwareengineering.stackexchange.com/questions/277250
-
07-10-2020 - |
Question
I have an xml file format that looks something like this:
<topLevelTag>
<category name="foo">
<item attrib="value">ItemName</item>
<item attrib="value2">AnotherItem</item>
</category>
<category name="bar">
<item attrib="value">ItemName</item>
<item attrib="value2">AnotherItem</item>
</category>
</topLevelTag>
The code that parses this creates an instance of ItemName
and is told that it's one category is, in the first case, "foo"
, and then creates a second instance of ItemName
which has associated category "bar"
.
The problem is that I need to redesign the system so that each Item
can have more than one Category.
But I still need to be able to create multiple items as well. For example (using bullets instead of xml) I might need to create the following instances:
ItemName
- Primary Category -
"foo"
- Secondary Category -
"bar"
- Primary Category -
ItemName
- Primary Category -
"bar"
- Secondary Category -
"foo"
- Primary Category -
ItemName
- Primary Category -
"foo"
- Secondary Category -
"quux"
- Primary Category -
AnotherItem
- Primary Category -
"baz"
- Secondary Category -
"foo"
- Tertiary Category -
"monkey"
- etc ... (no cap)
- Primary Category -
StillAnotherItem
- etc ...
- etc ... (no cap)
How can I design my XML format to encapsulate this many-to-many relationship?
I am aware this question is on the borderline between StackOverflow and Programmers. I chose to put it here because it is a software architecture and design question. If not please let me know and I'll be happy to move it. Precedent one | Precedent two | Precedent three
No correct solution
OTHER TIPS
Why can't you use the same structure as in your bullets?
<topLevelTag>
<item id="1">
<value>ItemName</value>
<category level="primary">foo</category>
<category level="secondary">bar</category>
</item>
<item id="2">
<value>ItemName</value>
<category level="primary">foo</category>
<category level="secondary">bar</category>
</item>
</topLevelTag>
(the "id" distinguishes two instances of the same itemName).
For sure, you can't have a "many to many" structure explicitly - you must pivot (root?) either on item, or on category, since you need a hierarchical data set.
Or you change completely layout and pivot on the many-to-many relationship; this is easiest to convert to and from a RDBMS storage.
<topLevelTag>
<item id="1">...</item>
<category id="1">foo</category>
<mapping>
<map item="1" category="1" />
<map item="1" category="2" />
<map item="2" category="2" />
</mapping>
</topLevelTag>