Many of the latest-generation Web applications increasingly allow users to define parts of an application's data model. For example, address book applications routinely allow users to define custom properties associated with an address. Inventory or online shopping catalogs might also allow user-defined properties or fields. Such customization abilities directly facilitate user-generated content, since users are often in the best position to decide what pieces of data are important to their use of an application.
Although traditional relational schema are able to accommodate such custom properties with joins, loosely structure data can also be represented with XML natively in the latest-generation RDBMs.
In a recent four-part IBM developerWorks article, XForms and Ruby on Rails at the doctor's office, Tyler Anderson and Michael Galpin demonstrate not only how to efficiently store and query XML-based data in a relational database, but also how to keep the XML-based data in every layer of the application, including the UI.
Although the article uses Rails as a server-side technology, most of the concepts can easily be used in other environments, such as Java using JDBC 4.
A key benefit of Anderson's article is that it shows with examples how to use SQL queries alongside XQuery, the XML query standard. The XML data is stored in a column with an XML data type, and an SQL/XML query is used to retrieved the XML based on criteria from the XML structure:
Instead of mapping an XML document directly to a table, you map it to a column. A given row in your table could even have multiple XML documents... You can insert data into the table using SQL. You can treat the XML as a string when writing the SQL...
You write it as a string with a certain syntax that allows the database to parse it into the specialized data type. If your syntax is off, then the query will fail... You can also query XML by using SQL and XQuery:
SELECT XMLQUERY('<Patients>
{for $i in $x/Info
where $i/Insurer = "Blue Armor"
return <Patient>{$i/FirstName/text()}</Patient>}
</Patients>'
passing P.INFORMATION as "x") from DOC.PATIENTS P;
The key is using the XMLQUERY reserved word to indicate an XQuery that is executed against an XML document that is passed in via the SQL select statement.
The rest of Anderson's article focuses on using XML on the client via the XForms standard:
It allows you to define your data in a simple XML model and your view using standard HTML form elements. XForms then provides declarative mapping between these elements. That means you will not have to write either client-side or server-side code for taking some submitted value and inserting into an XML structure. XForms handles it for you. It even does all of this asynchronously: changes in the HTML form are bound to the XML model and sent to the server for synchronization. You get the benefits of Ajax without having to write any JavaScript...
You also have a submission that is part of the model. The submission will take your model instance and post it to the URL specified in the submission action attribute. In the body of the form, you have a series of XForms elements.
Anderson also describes how to validate data on the client with XForms' support for XML schema.
What do you think of storing XML in a database and then querying it via SQL/XML?