Presents your
XML E-NEWSLETTER for June 25, 2003
<------------------------------------------->
USING THE MARKETPLACE DESIGN PATTERN
When designing XML solutions, the most obvious answer isn't always the
most correct answer. One instance where this is true is the case of the
Marketplace design pattern. This pattern solves some basic design
issues for usability in XML documents.
THE BASIC PROBLEM
The Marketplace pattern helps solve a basic problem with XML documents.
Imagine you have a document with a hierarchy of elements, such as a
family tree. In LISTING A, we see a family tree that contains two
Parent elements and one Child element.
The problem is that, using this hierarchy, it's difficult to place the
Child element in the structure using a simple parent-child hierarchy.
That's because a human child (unlike an XML child) is required to have
two parents rather than just one.
Listing A: marketplace_problem.xml
Dad
Mom
John
This type of problem exists any time you have a set or series of
elements that can be classified in a hierarchy in multiple ways.
THE MARKETPLACE PATTERN
The essence of this design pattern is that it allows you to regroup and
reorganize your data hierarchy to solve this problem. In the above
example, you may be tempted to place the Child element under one of the Parent
elements; however, you'll quickly realize the difficulty in processing
and understanding this type of data. Placing the Child under the Dad
element removes the relationship to the Mom element and vice-versa.
Instead, we'll implement the Marketplace pattern. This solution uses the
XML feature called attributes to remove the inadequate hierarchy
structure, yet it still retains the important relationship data. Rather than
place the Child element under either the Mom or Dad element, you leave it
where it is and add additional attributes to indicate the structure.
THE SOLUTION
In our example, we want the data to communicate that there is a family
that consists of a mom, a dad, and a child named John. We also want to
show explicitly that John is the child of Mom and Dad--a relationship
that is only implied in LISTING A.
In order to explicitly show this relationship, we won't make the Child
element a child of either Parent element. Instead, we'll leave it where it
is, but add some attributes to describe the structure. This approach is
shown in LISTING B.
Listing B: marketplace_solution.xml
Dad
Mom
John
In this example, you can clearly see that John has a mom called Mom and
a dad called Dad.
There are multiple approaches to creating an explicit solution. Instead
of identifying the structure by name, you could use identifier tags.
Also, instead of creating explicitly structured attributes, you could create
a more generic attribute and use a list. You can see a combination of these
approaches in LISTING C.
Listing C: marketplace_solution2.xml
Dad
Mom
John
This approach allows you to create more sophisticated data structures.
For instance, in today's world, a child may have two fathers or two
mothers rather than one of each; the solution in LISTING C allows you
to easily model such a family.
SUMMARY
When designing XML documents, you often encounter data structures that
don't fit easily into an XML model. The Marketplace pattern is used to
solve some of these seemingly simple XML data structure problems.
By using the Marketplace pattern, you can create more complex XML
documents that use multiple hierarchies.
Brian Schaffner is an associate director for Fujitsu Consulting. He provides
architecture, design, and development support for Fujitsu's Technology
Consulting practice.
----------------------------------------