Presents your XML E-NEWSLETTER for June 11, 2003 <-------------------------------------------> SOLVE COMPLEX PROBLEMS WITH THE MIXED CONTENT ELEMENT When developing XML solutions, you're often faced with difficult decisions. These decisions frequently revolve around how to structure the data within your document. Choosing one solution makes sense in one situation, while another solution makes sense in another. We'll examine one scenario where you might have to make this decision and demonstrate an elegant pattern that helps solve it. THE COMPLEX CONTENT PROBLEM Imagine that you aren't entirely certain what kind of data you're going to put inside a particular element. In fact, on top of being unsure what the data is, you're also uncertain how the data is structured. This situation could create real problems for your document structure. Let's take a look at an example. Suppose you have an element called Item in your XML document. For whatever reason, the data that goes into this element may come from a variety of sources (such as different divisions or a merger). Because the data comes from different sources, it may be in a predictable structured format, or it may be loose text that describes the item. The problem is: How do you describe both at the same time? THE MIXED CONTENT SOLUTION One solution is to use a mixed content element. The mixed content element is a simple concept that allows you to put both structured and unstructured data into a single element, simultaneously. The mixed content element is defined in the Document Type Definition (DTD). LISTING A shows a sample DTD that illustrates how to define a mixed content element. Listing A: mixedcontent.dtd This code has been simplified. It shows that an Order element can contain one or more Item elements. Each Item element can then contain text data, or a child Description element. Because of the nature of mixed content elements, you have less control over what kind of structured data can appear. THE PROBLEM AND THE SOLUTION Let's look at the problem in a little more detail and discuss a potential solution. Suppose we have an order, as described above, that might contain both "raw" content and structured XML content. LISTING B shows an example of how an actual XML document might look. Listing B: order.xml KKU8123 Super Widget A super widget device 13.50 8234556:Hyper Flange, $34.95 Small metallic device for assisting in flotalating. Metallic Flotalator .50 Our order contains multiple items, with the item data coming from different systems and in different formats. The good news is that we can use a mixed content element to describe each item without breaking our XML document. LISTING C shows an extension of the DTD above, including the SKU, Name, Description, and PricePer for each item, in addition to any raw content. This DTD will serve fine to describe the XML document in LISTING B. Listing C: mixedcontent2.dtd SUMMARY When working with XML data, you must often determine the best way to organize data that you don't have much control over. One solution to the problem of disparate data formats is to use a mixed content element, which can include both raw data and structured child elements. Brian Schaffner is a senior consultant for Fujitsu Consulting. He provides architecture, design, and development support for Fujitsu's Telcom360 group. ----------------------------------------