?
Solved

Return attribute nodes from multiple element nodes using SQL 2008 and Xquery

Posted on 2011-03-17
5
Medium Priority
?
780 Views
Last Modified: 2012-05-11
I have a table in SQL 2008 which contains one column as an xml data type.

This table has thousands of rows and for that xml column the xml data in that field would have a stucture like follows:

<client_info first_name="John" last_name="Doe">
<court date="08/01/2010">
<cs date="09/01/2010" />
<cs date="10/01/2010" />
</court>
<meeting date="08/05/2010">
<teacher date="08/06/2010" />
<teacher date="10/18/2010" />
</meeting>
</client>

How can I return, preferably as a single XML datagram, a document that has the value of every date attribute and the local-name of it's parent node bearing in mind this is just a simple XML example and there can and will be more levels to this document and it will need to be processed for each row on the table ideally returning a single XML datagram.

Any SQL/XML experts who can share a solution I would greatly appreciate it.
0
Comment
Question by:hyphenpipe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
5 Comments
 
LVL 6

Author Comment

by:hyphenpipe
ID: 35161119
The best I have come up with so far is:

select      t.c.value('local-name(.)', 'varchar(75)')
            , date = t.c.value('@date', 'datetime')
 from table
 cross apply column.nodes('//*') t(c)
 where t.c.exist('//@*[local-name(.) = "date"]') = 1

But that returns every node name in for the XML document if at has at least one date.

I'd like to return on one shot just every date and its parent node name.
0
 
LVL 6

Author Comment

by:hyphenpipe
ID: 35161127
Also, I notice my XML is not well formed.

The starting element node should be <client... and not <client_info...

I think you get the rest.
0
 
LVL 6

Author Comment

by:hyphenpipe
ID: 35161141
As an update, here is what I have so far:

declare @doc xml
set @doc = '<client first_name="John" last_name="Doe">
<court date="08/01/2010">
<cs date="09/01/2010" />
<cs date="10/01/2010" />
</court>
<meeting date="08/05/2010">
<teacher date="08/06/2010" />
<teacher date="10/18/2010" />
</meeting>
</client>'

select @doc

select      name = node.c.value('local-name(.)', 'varchar(75)')
            , date = node.c.value('@date', 'datetime')
 from @doc.nodes('//*') node(c)
 where node.c.exist('//@*[local-name(.) = "date"]') = 1
 for xml auto

Doing so returns this:

<node name="client" />
<node name="court" date="2010-08-01T00:00:00" />
<node name="cs" date="2010-09-01T00:00:00" />
<node name="cs" date="2010-10-01T00:00:00" />
<node name="meeting" date="2010-08-05T00:00:00" />
<node name="teacher" date="2010-08-06T00:00:00" />
<node name="teacher" date="2010-10-18T00:00:00" />

But I would like to eliminate that first node (client) before returning the XML datagram.

Again, I will also need to process this for a large table with thousands of rows but just one XML column.
0
 
LVL 6

Accepted Solution

by:
hyphenpipe earned 0 total points
ID: 35161177
Got it.

select      name = node.c.value('local-name(.)', 'varchar(75)')
            , date = node.c.value('@date', 'datetime')
 from @doc.nodes('//*[@*[local-name(.) = "date"]]') node(c)
 for xml auto

Thanks.

0
 
LVL 6

Author Closing Comment

by:hyphenpipe
ID: 35161179
Figured out my own solution.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question