Presents your XML E-NEWSLETTER for August 28, 2002 <-------------------------------------------> KEEP XML SIMPLE XML can be an immensely complex and difficult technology to work with. It can also be very elegant and easy. The difference may not be based on your experience with XML or data modeling but rather on the inherent complexities built into your solution. Let's look at some techniques you can use to keep your XML simple. TOO MANY CONTAINERS A container element is one that holds a set of like elements. For example, an order may contain five items. Those items may just be elements within the order element, or they may all be contained by an items element. With the container, it looks like this: If the Items element simply contains the Item elements and doesn't provide any extra information, it's probably easier to not have the Items element. Instead, the Item elements are just children of the Order element, like this: TOO MANY ELEMENTS There are also times when XML documents seem to have too many elements. There may be ways to break data down into very small compact units; however, there isn't always a necessity to do so. Take, for example, a date field. It can be expressed in many ways in XML: 2/5/99 2 5 1999 19 99 02 05 Friday nineteen-hundred and ninety nine February fifth twelve thirty twenty For certain situations, you may need a complex element design; however, most situations probably don't warrant this approach. By keeping the number of elements to a minimum, you can maintain a fair level of simplicity in your XML documents. CONCISE ELEMENT NAMES Creating a scheme for element names can be an important part of your XML standards. A common problem with element names occurs when they're compounded throughout the document tree. For example, imagine an order that contains a customer who has a shipping address. While this is perfectly valid XML, it's a somewhat cumbersome format, especially if you need to go to deeper levels in your tree. It's also somewhat redundant. A simpler approach might look more like this:
This format is not only simpler and easier to read, but it also allows you to reuse element definitions. Instead of having an OrderCustomerShippingAddressZip element, which you use only in this spot, you now can use a generic Zip element and not worry about new element definitions. Brian Schaffner is a senior consultant for Fujitsu Consulting. He provides architecture, design, and development support for Fujitsu's Telcom360 group. ----------------------------------------