Presents your XML E-NEWSLETTER for July 31, 2002 <-------------------------------------------> REAP THE MOST BENEFIT FROM STRUCTURAL DESIGNS IN XML One of the most common adjectives used to describe XML documents is structured. But what is structure and how does it contribute to the overall goal of XML documents? Let's examine how you can make the most of structural designs in your XML documents. GETTING ORGANIZED Structure is often defined as organization. That really just means the arrangement of items in the place where they go. But the question is, "Where do these items go?" When we talk about XML documents and their relative structure, we really mean the way that elements are grouped together both in a sibling relationship and in a parent/child relationship. The first part of getting organized with XML is understanding when to use which relationship. A LESSON IN PARENTING Sibling elements in an XML document are those elements that share the same parent element. It's possible to have an XML document that has no sibling elements. For example: As you can see, the parent element has an only child, who in turn bears an only grandchild. It's possible, although merely theoretically, to have no child elements: As you can see, sisterMary is the root element and has neither child nor sibling elements. It's important to remember that the rules of XML specify that each XML document must contain at the very least a root element. SIBLING RIVALRY Now let's get back to our discussion of siblings. Each sibling shares the same parent. Therefore, to have siblings, you start with a parent and make two child elements below it: Both childOne and childTwo are child elements of the parent element. Also, childOne and childTwo are sibling elements that share the same parent. FAMILY LIFE CAN BE COMPLICATED So far, our example is quite simple and makes sense (for the most part). But let's complicate the issue a bit. What if our parent element contains things other than child elements? Also, we only have a single parent. Most kids come from two parents. Let's say that these parents also have two dogs, a cat, and each have a car. Describing these can be easy or difficult depending on how you structure them in the XML document. Let's start with the major problem of having two parents. XML doesn't allow an element to have two parents. Each element (unless it's the root element) must have one, and only one, parent element. In order to describe our family, we'll have to apply a different technique. CHANGE YOUR PERSPECTIVE TO SEE A NEW STRUCTURE One way to think about the problem is to change our perspective. Rather than the kids being children of the two parents, let's imagine all of these people belonging to a family. Now, our XML jargon for child and parent no longer applies to the parents and children in the family but to the elements used to describe our family. To clarify, imagine that we have a root element called family and the parents are children of the family element: The kids are no longer children elements of the parents but are part of the family. They are children of the children element: Jane Mercury Villager John Mazda Miata Johnny Jr. Sally Fido Spot Bartholomeow Notice that we've changed several aspects of our structure. Our parents aren't just "mom" and "dad". But rather, they can be described generically as parent elements, each with their own name and car. The same with the children--they can be described with generic child elements belonging to the children element. In a basic sense, our family is composed of a parent element, a children element, and a pet element. Each of these elements aggregates the set of similar elements and creates a simple but powerful structure that can be applied to many actual family structures. Brian Schaffner is a senior consultant for Fujitsu Consulting. He provides architecture, design, and development support for Fujitsu's Telcom360 group. ----------------------------------------