Last Thursday I went to MAVBUG to do a presentation on XPath and XQuery. We implemented XQuery in SQL Server 2005 so that people could query XML stored in the database. XQuery has the same relationship to XML that SQL does to relational data. That's especially true in Microsoft's implementation, as we've extended XQuery to include XML-DML, a data manipulation language for XML. (The W3C standard is just a query tool, we added commands like insert, delete, and modify.)

Here's an example:

SELECT Instructions.query('declare namespace AWMI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";        
    /AWMI:root/AWMI:Location[@LocationID=10]
') as Result
FROM  Production.ProductModel
WHERE ProductModelID=7

This is a SQL SELECT statement, but we've added some XQuery that selects the XML element with a LocationID attribute of 10. The XQuery is inside the Instructions.query function, and everything outside the function is in SQL.

I was trying to do something a little different, by avoiding a product demo and drilling into the XQuery language instead. I had mixed results; some folks got it, but I think I went straight other some folks heads. There's no doubt that when you see it for the first time, XQuery seems complex. I probably over-estimated people's experience with XML. I just assume that everyone lives and breathes XML all the time, and it turns out that may not be true.

I also got some questions about the user experience. People, it's a programming language! No, end users can't write XQuery. Most end users can't write SQL either, and yet somehow relational databases are still useful.

My slides are here. The sample queries are here. (Right click on the samples link, choose Save As, and then rename the file to .SQL.) If you want to play around with XQuery, these samples should work in any recent build of SQL Server 2005. They aren't deeply original, many of them were ripped off from books online, but I've made a convenient file for you to load in Management Studio, saving YOU, my beloved reader, from some cut and paste.

I'll be repeating this presentation next week at the Data Management Association in Richmond. They are meeting at the Dominion Resource building in Innsbrook. The meeting starts at 4:30 PM, and I'm speaking at 5:30 PM.

Here's an abstract: Querying XML Using XQuery

It’s impossible to overstate the importance of XML as a data format. This presentation will drill down into XPath and XQuery, two of the standards from W3C that support XML. XPath allows you to identify a sub-node within an XML document. It’s the standard format for searching XML documents. XQuery builds on XPath to allow selection and manipulation of sections within an XML document. Microsoft has adopted XQuery as the query language for XML documents stored in SQL Server 2005. When you read XML data from a SQL Server 2005 database, you’ll typically need to use a combination of SQL and XQuery to select the XML that you require.

In this session, we’ll talk about the theoretical underpinnings of the two standards, and demonstrate numerous examples based on Microsoft’s implementations of the XPath and XQuery standards.

MAVBUG's next presentation is Woody Butler talking about DotNetNuke. It should be a great introduction to DotNetNuke for folks who haven't seen it before.