Reputation: 2550
I am working on SQL query where i need to parse xml to sql .Query is working for other xml but not working for below xml. query i am using is as below
DECLARE @xmldata XML
SET @xmldata =
N'<SearchProductsResponse xmlns="http://api.abc.com/">
<productItems>
<productItem id="5d0ee86d84bcc5edef43236d61419a59">
<trackingLinks>
<trackingLink adspaceId="100">
<ppv>
abc.com
</ppv>
<ppc>
abc.com
</ppc>
</trackingLink>
</trackingLinks>
</productItem>
</productItems>
</SearchProductsResponse>';
select
t1.c.value('@id', 'varchar(300)') as itemid,
c.c.value('@id', 'int') as adspaceId
from
@xmldata.nodes('*:SearchProductsResponse/*:productItems/*:productItem') as t1(c)
OUTER APPLY t1.c.nodes('*:trackingLinks/*:trackingLink') as c(c)
Output I am getting is
itemid adspaceId
5d0ee86d84bcc5edef43236d61419a59 NULL
But I should return 100 instead of NULL
. This query is working for other XML but don't know whats wrong with this XML. I have double check XML and query nothing different from other XML. Hope I am not missing some silly mistake
Upvotes: 0
Views: 47
Reputation: 908
Change the attribute selection for your adspaceId column to @adspaceId
.
select
t1.c.value('@id', 'varchar(300)') as itemid,
c.c.value('@adspaceId', 'int') as adspaceId
from @xmldata.nodes('*:SearchProductsResponse/*:productItems/*:productItem') as t1(c)
OUTER APPLY t1.c.nodes('*:trackingLinks/*:trackingLink') as c(c)
Upvotes: 1