SQL Server 2005 came up with a suite of exciting XML features. I am pretty sure that most developers working with SQL Server 2005 must be using the XML support. In the past few years, I had seen a lot of discussion (or would you call it a debate?) about XML support at the database level. Some people argue that XML is not needed at all, where as some argue that XML can make your life better.
I am from the latter group. I admire the XML capabilities of SQL Server 2005. My team makes a good use of the XML capabilities of SQL Server 2005. However, the intention of this post is NOT to advocate for the use of XML. I do believe that there is no single solution to any given problem. The solution varies depending on the angle that you take to analyze it and the circumstances, availability of resources etc.
If you are in-charge of designing a solution to a given problem, you do you have your freedom to take the approach that you feel is better. You must be having your own reason for that. And you will be right, as long as the problem is solved in the acceptable level. A given problem may be solved by 10 different people in 10 different ways. But if the quality of the result is acceptable, we should count all of them to be correct.
I found several interesting comments on this topic at the discussion forums of SQL Server Central. Many of my XML articles witnessed interesting comments where many database experts expressed their views on the usage of XML. I think the answer is so simple. If XML helps you to solve a certain problem better, go for it. Otherwise, stick to a non-XML approach. Whenever you find a problem which can be better solved with the usage of XML, use it. I have come across several situations where the usage of XML was found to be better choice for a variety of reasons. Some of my articles at SQL Server Central covers a few such scenarios.
I think before we argue on "XML" or "NO XML" we should look at the XML capabilities of SQL Server 2005 from a neutral angle. It makes sense to understand the XML features in detail. Once we have a very good understanding of what SQL Server 2005 can do with XML, we will be able to decide whether some of those capabilities can be useful to the problems that we are trying to solve in our day-to-day programming life.
I would classify the XML support extended by SQL Server 2005 into 4 broad categories.
- Generating data in XML format. Many applications today need to transform relational data in to XML format. The best example is web sites which generates XML feeds. Almost all Web 2.0 web sites today publish RSS or ATOM feeds. This involves reading relational data from the databases and generating the XML out put as per the specified feed format. Service Oriented Architecture (SOA) is getting more and more popularity and applications that communicate with other applications need to exchange data in XML format. This is shows the importance of having the capability to generate XML result sets. SQL Server 2005 supports generating XML results from relational data by using FOR XML keyword. This may be used with AUTO, RAW, PATH or EXPLICIT. Each of the above keywords give you different level of control over the structure of XML output being generated.
- Validating XML data. Once you start using XML data extensively, the validation requirements will arise. You need to validate the structure of the XML as well as the quality of the data. If your application is accepting XML data from others, you need to make sure that the data exchanged is as per the required structure. Further, the values need to be validated as well. With SQL Server 2005, you can define an XSD schema to validate the structure and quality of the XML data being processed. You can create XML SCHEMA COLLECTIONS in SQL Server 2005. An XML column or variable can be associated to an XML schema which validates the XML.
- Indexing XML Columns. An XML column can store large XML documents. XML documents are stored as BINARY objects. Searching for values in an XML column might be time consuming if the XML documents are big and the table has too many records. By creating correct indexes on XML columns, you can make your XML Queries faster. SQL Server 2005 supports XML indexes.
- Querying XML data. If you have columns of XML data type, it is apparent that you will have to query the XML data and extract specific values from the XML document. SQL Server 2005 supports XQuery, the most popular XML query language. XQuery gives you enough freedom to query the XML data and extract the specific piece of data that you need.
It would be helpful to have a closer look into each of those areas and you might realize that some of those features can make your life easier. For example, if your application generates RSS or ATOM feeds, you might find it easier to work at the database level, rather than at the application level. Most application that generates feeds today, read data from the database and generate XML data at the application level. A typical .NET application might retrieve an ADO.NET DataSet from the database and might use the .NET XML classes to generate the specific version of the feed. Some of you might feel that it would be better to do this at the data base level. Instead of returning a normal result set to the application and having the application do the transformation, some of you might find it interesting to do with FOR XML.
So friends, I invite all of you to dive more into the XML capabilities of SQL Server 2005 and see if you can find something that will make your day-to-day programming life easier and make you more productive.