My goal is to break out this xml so I can take the Field elements for each Record Row and insert into a SQL table. I cannot figure out how to traverse the Grids hierarchy at all. I have been able to run a few xquery commands against it, but only at the highest level as shown below. Please help to get all the way down do the Field Element values for each field for each Row.
declare @Responsetext varchar(8000)
set @Responsetext =
'<GridResponse xmlns="http://abcd.abcd.net/aapi/2009/08/">
<Brand id="1111">API Starter App</Brand>
<User>AAA_API</User>
<Grids>
<Grid type="subscriber">
<Record row="1">
<Fields>
<Field element="subscriber_id">111107293</Field>
<Field element="bounce_date" />
<Field element="cancellation_mailing_instance_id" />
<Field element="cancellation_message" />
<Field element="cancellation_date" />
<Field element="email">aaaa4@gmail.com</Field>
<Field element="is_repeated_bouncer">0</Field>
<Field element="is_unsubscriber">0</Field>
<Field element="modified_date">2015-04-07T13:19:09.3400000Z</Field>
<Field element="service_since_date">2011-10-17T13:23:38.7800000Z</Field>
<Field element="user_id" />
</Fields>
</Record>
<Record row="2">
<Fields>
<Field element="subscriber_id">111169135</Field>
<Field element="bounce_date" />
<Field element="cancellation_mailing_instance_id" />
<Field element="cancellation_message" />
<Field element="cancellation_date" />
<Field element="email">aaaa.bass@aaa.org</Field>
<Field element="is_repeated_bouncer">0</Field>
<Field element="is_unsubscriber">0</Field>
<Field element="modified_date">2014-12-15T17:30:18.2230000Z</Field>
<Field element="service_since_date">2011-10-19T14:11:26.6370000Z</Field>
<Field element="user_id" />
</Fields>
</Record>
</Grid>
</Grids>
</GridResponse>
'
This code gives me the node tree, but I cannot figure out how to tweak this to isolate the Field Values that are 8 levels deep.
--This CTE gives you the Node Tree
WITH Xml_CTE AS
(
SELECT
CAST('/' + node.value('fn:local-name(.)',
'varchar(100)') AS varchar(100)) AS name,
node.query('*') AS children
FROM @XmlResponse.nodes('/*') AS roots(node)
UNION ALL
SELECT
CAST(x.name + '/' +
node.value('fn:local-name(.)', 'varchar(100)') AS varchar(100)),
node.query('*') AS children
FROM Xml_CTE x
CROSS APPLY x.children.nodes('*') AS child(node)
)
SELECT distinct name
FROM Xml_CTE x
OPTION (MAXRECURSION 1000)
If I shove the xml into a table instead of using the variable, I can get the root node like this, but still cannot get down to the field level.
--Parent Node -- Returns GridResponse
SELECT
(
SELECT
c.value('local-name(.)', 'nvarchar(50)')
FROM
xmldata.nodes('/*') AS r(c)
) AS RootParent
FROM [dbo].subscriber_xml
I can query to see if the nodes exist, and get the root and the Brand child node, but not the Grids child node. This is very confusing why one works and the other does not.
SELECT XmlData.exist('(/*[1][contains(local-name(.),"GridResponse")])')
FROM [dbo].subscriber_xml
SELECT XmlData.exist('(/*/*[1][contains(local-name(.),"Brand")])')
FROM [dbo].subscriber_xml
SELECT XmlData.exist('(/*/*[1][contains(local-name(.),"Grids")])')
FROM [dbo].subscriber_xml