Presents your
XML E-NEWSLETTER for September 18, 2002
------------------------------------------
INVERT DOCUMENTS TO GET A DIFFERENT VIEW
XML documents can be constructed in many different ways, and you can use
XSL to translate from one format to another. However, it can often be
difficult to arrive at a solution that's elegant, simple, and functional.
Here's one approach that will change the way you look at your XML
documents.
DEFINING A PROBLEM
The first thing we need is a problem to solve. If you think about a
family tree, it provides a good metaphor for working with XML documents and
concepts. The problem is that you can't really show an entire family tree
with an XML document because each XML document has only one root (or
ultimate parent), while people always have two.
It's possible to define the document root as something abstract that
contains the two ultimate parent nodes. Consider the following:
With this approach, you can show all of the Smiths' children, their
grandchildren, their great-grandchildren, and so on. But what if we
think about the problem a little differently?
A NEW LOOK
What if instead of thinking about the tree from the perspective of the
ultimate root, we think about it from the perspective of the ultimate
child? One problem with the above parent node example is that it limits
us to a single ancestral point. When we discover more ancestors,
we may have a problem.
Another way to go about solving the family tree problem is to change
the
way we view the XML hierarchy. Instead of making family ancestors XML
ancestors, let's try making them descendants.
Let's look at a sample XML document that illustrates this technique.
LISTING 1 shows the ancestors for a fictional John Smith.
LISTING 1: johnsmith.xml
A NEW AXIS, A NEW ANGLE
Our sample document has a root element called JohnSmith, and what you
see in the document are his ancestors. In terms of XML hierarchy, they
are also his children. Each node in this document contains two children
nodes that represent the parents of the parent node. Confused yet?
Here's a more basic illustration:
Each person's child elements are actually their parents. Each parent
element's parent is actually the child. We have inverted the document
so that we can see John Smith's ancestors more clearly.
SUMMARY
What we've done is basically alter our approach to the problem. Our new
approach has its own disadvantages, such as not being able to show an
ancestor's siblings. It does, however, show the direct line of ancestors,
which can be easily traversed as XML children nodes. You can also add
ancestors to this model without breaking any of your XPath statements,
as you would with our first example.
Brian Schaffner is a senior consultant for Fujitsu Consulting. He
provides architecture, design, and development support for Fujitsu's
Telcom360 group.
----------------------------------------