XML and Data Modeling
A lot of attention is given to what XML looks like and how it is structured,
not as much is spent on creating XML data structures or XML "data modeling".
Welcome to my humble attempt.
 | Data modeling is the process in which you develop a plan to organize your
information. |
 | Traditionally, this has been used in designing and implementing
databases. Techniques such as entity-relationship and semantic object
modeling have been used for this purpose. |
 | XML data structures are simpler and more like tables in relational
databases. |
One of the quickest ways to create an XML data model that reflects a database
table is to simply copy it. For example:
 | Table: customer with the following fields:
 | lastname |
 | firstname |
 | address |
 | city |
 | state |
 | zip |
|
 | The XML structure would look like this... |
<customer>
<lastname></lastname>
<firstname></firstname>
<address></address>
<city></city>
<state></state>
<zip><zip>
</customer>
This works for most simple table to XML correlations.
Now imagine a join of the customer data with the transaction table and
associated tables (in other words, a full blown commerce data model):
 | Another table: transaction that includes
 | customer_id |
 | transaction_date |
 | payment_type |
 | detail_id |
|
 | Linked to another table: detail
 | detail_id |
 | product_id |
 | qty |
 | price |
|
 | Linked to the product table
 | product_id |
 | product_name |
|
The XML for this would look like this...
<transactions>
<transaction>
<trandate>11/7/2003</trandate>
<paymentmethod>Visa</paymentmethod>
<customer>
<lastname>Smith</lastname>
<firstname>John</firstname>
<address>111
Elm St</address>
<city>Anycity</city>
<state>MD</state>
<zip>20888</zip>
</customer>
<details>
<detail>
<productname>battery</productname>
<qty>2</qty>
<cost>$2.99</cost>
</detail>
</details>
</transaction>
</transactions>
How does this work?
 | The root element contains all transactions. |
 | Each transaction includes the customer data (name, address, etc.),
details, date, and payment. |
 | The details handle one or more products. |
 | Product information includes name, quantity, and price. |
Is there any other way to accomplish the above design?
 | Yes -- as long as it works as desired, and... |
 | Follows all the rules for well-formed XML. |
Tips for Designing XML Data Models
 | Look at a lot of different XML models and understand how they work. |
 | Break the model down into chunks that make sense. |
 | Organize like data under a common element (e.g., customer contains,
lastname, firstname, etc.). |
 | Understand the repeating elements (e.g., the transactions, customers,
products). |
 | As you look at repeating elements, make sure they repeat under the proper
parent elements. |
 | Practice, practice, practice. |
|