SQLAuthority News – Guest Post – SELECT * FROM XML – Jacob Sebastian

One of the most common problem SQL Server developers face while dealing with XML is related to writing the correct XPath expression to read a specific value from an XML document. I usually get a lot of questions by email, on my blog or in the forums which looks like the following:

“I have the following XML document and I am trying to read the value from xyz node. When I run my query, I get a NULL value”

My friend Jacob Sebastian (SQL Server MVP) has written excellent article on the subject SELECT * FROM XML, I strongly recommend to either bookmark it and read it before continuing further in this article.

In most cases, I have seen that the problem was due to an incorrect XPath expression. XPath expressions are not complicated at all, but they need a close attention to get them right. Assume that we have the following XML fragment.

SQLAuthority News - Guest Post - SELECT * FROM XML - Jacob Sebastian xmlpost1

Looking at the structure of the XML document, it is quite easy to figure out the XPath expression pointing to each element and attribute. The following illustration shows how to do this.

SQLAuthority News - Guest Post - SELECT * FROM XML - Jacob Sebastian xmlpost2

One of the simplest ways is to identify the nodes with their position as given in the above illustration. A more detailed listing is given below.

SQLAuthority News - Guest Post - SELECT * FROM XML - Jacob Sebastian xmlpost3

The above example also used the “position” of the elements to uniquely identify them. In real life you might need to have more complex matching criteria such as the “email of the Employee element whose id is 10001”. The following example shows how to apply this type of filters.

/Employees/Employee[@id=”1001″]/Email

In most cases, you will be able to easily build your XPath expressions. However, if you find it difficult, you can take help from my helper function given here. This function allows you to run ‘blind’ queries on the XML document very similar to the ‘select * from table’ queries that we usually run on unknown tables.

For example, if you would like to quickly examine the above XML document and see the elements, attributes and their XPath expression, you can execute something like the following:

SQLAuthority News - Guest Post - SELECT * FROM XML - Jacob Sebastian xmlpost4

Make sure that you create the function XMLTable() using the script given in the above URL.

Once you have the output of the function, you can copy the XPath expressions from the results and use in your Queries. For example, if you are looking for the email address of Pinal, you can just copy the expression from row 8 (highlighted in the image given above) and use in your query as:

SELECT @x.value('Employees[1]/Employee[2]/Email[1]','VARCHAR(50)')

I hope you will find this post interesting and the XMLTable() function might help you to solve some of the XML querying problems you may face in your SQL Server Journey. If you have got any question about XML in general, or about this function in particular, please feel free to post them on the XML forum and I will try my best to help you out.

Reference: Pinal Dave (https://blog.sqlauthority.com)

SQL Scripts, SQL Server, SQL XML
Previous Post
SQLAuthority News – Price List – Oracle vs SQL Server
Next Post
SQLAuthority News – Meeting Bryan Oliver and Learning Wisdom of Life

Related Posts

Leave a Reply