Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Parsing and joining XML using xquery

$
0
0
I have a table which has an xml column which contains multiple xml elements as follows:[code="sql"]CREATE TABLE ReportData ( id INT, ReportData XML );[/code]There is a second table called UIElements which lists a description of the xml names space descriptions. There is no physical relationship between this table or the first table:[code="sql"]CREATE TABLE UIElements ( id INT , ElementDescription VARCHAR(50) , Element VARCHAR(50) );[/code]Sample data of ReportData:[code="sql"]INSERT INTO dbo.ReportData ( id , ReportData )VALUES ( 1 , '<F7d34f773afe84c988756c0a5da129bed xmlns="http://www.Example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <DataType>date</DataType> <Value>2016-07-01T00:00:00Z</Value> <Archived>false</Archived></F7d34f773afe84c988756c0a5da129bed><F2257960efbc74b309adace69587a943b xmlns="http://www.Example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <DataType>list-reference</DataType> <Value>285</Value> <Archived>false</Archived></F2257960efbc74b309adace69587a943b>' );[/code]Sample data of Elements table: This is just one row, but in reality the table contains multiple rows for lots of different records, each with multiple namespace elements contained in the xml ReportData column.[code="sql"]INSERT INTO dbo.UIElements ( id , ElementDescription , Element )VALUES ( 1 , -- id - int 'Report Date' , -- ElementDescription - varchar(50) 'F7d34f773afe84c988756c0a5da129bed' -- Element - varchar(50) ), ( 2, -- id - int 'Report Number' , -- ElementDescription - varchar(50) 'F2257960efbc74b309adace69587a943b' -- Element - varchar(50) )[/code]I can use the following code to select and extract the <Value> element of the ReportData table for a specific element:[code="sql"] ; WITH XMLNAMESPACES ('http://www.example.com' AS ns) SELECT id, Valuexml = ReportData.ReportData.value('(/ns:F2257960efbc74b309adace69587a943b/ns:Value)[1]', 'varchar(1000)') FROM dbo.ReportData;[/code]However, as one row of the report data table can have multiple <value> elements for each of the different UIElements, I need to be able to select all iterations from the ReportData table. The output would look like this:ReportDataID Element Value1 F7d34f773afe84c988756c0a5da129bed 2016-07-01T00:00:00Z1 F2257960efbc74b309adace69587a943b 2852 F7d34f773afe84c988756c0a5da129bed 2016-07-02T00:15:00Z 2 F2257960efbc74b309adace69587a943b 290 3 F7d34f773afe84c988756c0a5da129bed 2016-04-01T00:00:00ZI'm struggling to work out how to select this data without running some kind of loop which reads through each UIElement, dynamically build the Xquerires, output the results to a temporary table, then select from that with an order by to get everything in order.Any tips?

Viewing all articles
Browse latest Browse all 3145

Trending Articles