Follow the rest of this series at the XQuery for the Non-Expert – X-Query Resources introduction post.
Let’s talk about XMLNAMESPACE. Are you excited? Does your skin crawl with excitement – or rather unease at the thought of leveraging this T-SQL clause?
The goal here is to make XQuery and it’s functions and clauses easier to swallow. Hopefully in a few examples and some analogies, you’ll be a pro at understanding them.
What is XMLNAMESPACE?
Well, let’s start with a definition. The one below is from Wikipedia:
XML namespaces are used for providing uniquely named elements and attributes in an XML document. They are defined in a W3C recommendation. An XML instance may contain element or attribute names from more than one XML vocabulary. If each vocabulary is given a namespace then the ambiguity between identically named elements or attributes can be resolved.
Ok, then. We’re non-experts here. Let’s use a definition that is a little more simple.
An XML namespace is a map of an XML document. It defines all of the possible attributes and values for each node. It defines each of the nodes. It also provides a logical design for what is possible within an XML document.
Yeah, yeah. Definitions. Let’s look at this from another angle. Suppose you had database schema similar to the model below. There are Customer, Address, Order, and Item tables.
Based on this model, we know a few things some things. The obvious is that there are four tables. But we also know that there are relationships between Customer and Address, Customer and Order, Order and Address, and Order and Item.
As you may know, information in these tables can also be represented in XML format. For a single customer, an XML document could look something like this:
<Address /> <Address />
The trouble with the above XML is that we don’t know anything about the structure of the XML besides the data that is in it. Do we know if there are limitations on valid values for State? Can there be multiple Addresses for an Order? These and other questions are unknown.
There needs to be something that defines what this structure is. As it is defined above, the XML NAMESPACE provides this information. It’s a virtual map to the XML structure that guarantees that each node, attribute, entity, etc. will behave in a specific way for all XML documents that it can be applied to.
Slapping It On
Hopefully at this point you understand the purpose of the XML NAMESPACE. The trouble is it’s pretty useless if we don’t know how to apply it to an XML document.
There are a few ways that the XML NAMESPACE can be applied to an XQuery statement:
- Assign it to a query and set it as the default namespace for the query
- Assign it to a query and provide an alias for the namespace in the query
- Assign it to an XQuery function and set it as the default namespace for the function
- Assign it to an XQuery function and provide an alias for the namespace in the function
These are all of the methods for assignment that I am aware of. For the sake of simplicity I’ll attempt to always focus on the first option when writing XQuery statements in this blog.
To use this method simply with the WITH clause at the start of your XQuery statement. The use the XMLNAMESPACE option and declare a DEFAULT on the URL of the XML namespace.
WITH XMLNAMESPACES(DEFAULT N'http://url/of/xml/namespace') SELECT Column1, Column2, … FROM dbo.SomeTable
Once this is completed you’ve done the work and all of the XML documents that are accessed will utilize this XML NAMESPACE
XML NAMESPACE is a component of XQuery that can provide a vital role in defining how to navigate an XML document. There are various methods to apply it to XQuery statements. As I noted above, I prefer one of the more simple and clean methods.
As I progress with this topic and with some posts that focus on querying the procedure cache with XQuery this should all come together. Until then, please leave your comments below. I will update this post from time to time to clarify on points.