Parsing XML using TSQL - sql

I'm trying to parse out the following XML with TSQL:
<Response xmlns="http://data.fcc.gov/api" status="OK" executionTime="9">
<Block FIPS="181770103002004" />
<County FIPS="18177" name="Wayne" />
<State FIPS="18" code="IN" name="Indiana" />
</Response>
Using the following script:
SELECT x.i.value('#name', 'varchar(200)') AS county
FROM #xml.nodes('Response/County') AS x(i)
But I get no results, any help as to what I'm doing wrong would be greatly appreciated.
Thanks!

Your XML namespace is messing things up. Either remove the xmlns="http://data.fcc.gov/api" from the Response element, or prefix your query with WITH XMLNAMESPACES ( DEFAULT 'http://data.fcc.gov/api')
;WITH XMLNAMESPACES ( DEFAULT 'http://data.fcc.gov/api')
SELECT x.i.value('#name', 'varchar(200)') AS county
FROM #xml.nodes('Response/County') AS x(i)
Or you can use wildcard namespaces in the query:
SELECT x.i.value('#name', 'varchar(200)') AS county
FROM #xml.nodes('*:Response/*:County') AS x(i)

You can do it using OPENXML like this:
DECLARE #idoc INT
DECLARE #xml AS XML =
'<Response xmlns="http://data.fcc.gov/api" status="OK" executionTime="9">
<Block FIPS="181770103002004" />
<County FIPS="18177" name="Wayne" />
<State FIPS="18" code="IN" name="Indiana" />
</Response>'
EXEC sp_xml_preparedocument #idoc OUTPUT, #xml, N'<root xmlns:n="http://data.fcc.gov/api" />'
SELECT
Name AS County
FROM OPENXML (#idoc, '/n:Response/n:County', 1)
WITH
(
Name VARCHAR(255) '#name'
)
EXEC sp_xml_removedocument #idoc
GO

Related

OpenXML returning NULL

I am trying to import xml into my database with the following query using OpenXML in Microsoft SQL Server:
DECLARE #xml XML;
DECLARE #y INT;
SET #xml
= '<ArrayOfArticle xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Article>
<ScriptId xmlns="https://test.com/">5135399</ScriptId>
<Title xmlns="https://test.com/">Stocks divided into two corners</Title>
<Mediatype xmlns="https://test.com/">News papeer</Mediatype>
<Abstract xmlns="https://test.com/">Foreign capital doubled this year.</Abstract>
<ScriptDate xmlns="https://test.com/">2017-12-30T00:00:00</ScriptDate>
<ScriptTypeId xmlns="https://test.com/">1</ScriptTypeId>
<ScriptType xmlns="https://test.com/">News general</ScriptType>
<Media xmlns="https://test.com/">Times</Media>
<ArticleUrl xmlns="https://test.com/">http://test.com</ArticleUrl>
<AnalysisResult xmlns="https://test.com/">
<Analysis>
<Regno>111</Regno>
<Name>New York Times</Name>
<Result>1</Result>
<ResultName>Positive</ResultName>
</Analysis>
<Analysis>
<Regno>222</Regno>
<Name>Washington Post</Name>
<Result>1</Result>
<ResultName>Negative</ResultName>
</Analysis>
</AnalysisResult>
<FacebookStats xmlns="https://test.com/">
<ShareCount xsi:nil="true" />
<LikeCount xsi:nil="true" />
<CommentCount xsi:nil="true" />
<TotalCount xsi:nil="true" />
</FacebookStats>
<MediaScore xmlns="https://test.com/">
<MediaScore>
<Regno>111</Regno>
<CompanyName>New York Times</CompanyName>
<MediaScoreID>2</MediaScoreID>
<Name>Neither</Name>
</MediaScore>
<MediaScore>
<Regno>222</Regno>
<CompanyName>Washington Post</CompanyName>
<MediaScoreID>2</MediaScoreID>
<Name>Neither</Name>
</MediaScore>
</MediaScore>
<Page xmlns="https://test.com/">26</Page>
<ProgramId xmlns="https://test.com/">0</ProgramId>
<ProgramTime xmlns="https://test.com/" xsi:nil="true" />
<ProgramLength xmlns="https://test.com/">0</ProgramLength>
<ProgramOrder xmlns="https://test.com/">0</ProgramOrder>
</Article>
</ArrayOfArticle>';
EXEC sp_xml_preparedocument #y OUTPUT, #xml;
SELECT *
FROM
OPENXML(#y, '/ArrayOfArticle/Article', 1)
WITH
(
ScriptId VARCHAR(20),
Title VARCHAR(30),
Mediatype VARCHAR(30)
);
The query however only returns NULL values. What am I missing here? Would it be optimal to import the XML using SSIS instead. Not sure how much more details I can give at the given hour.
Do not use FROM OPENXML. This approach (together with the corresponding SPs to prepare and to remove a document) is outdated and should not be used any more.
Try the XML type's native methods, in this case .value():
Your XML is rather weird - concerning namespaces. If its creation is under your control you should try to clean this namespace mess. The unusual thing is, that your XML declares default namespaces over and over.
You can use the deep search with // together with a namespace wildcard *:
--GetItEasyCheesy (not recommended)
SELECT #xml.value(N'(//*:ScriptId)[1]',N'int') AS ScriptId
,#xml.value(N'(//*:Title)[1]',N'nvarchar(max)') AS Title
,#xml.value(N'(//*:Mediatype )[1]',N'nvarchar(max)') AS Mediatype ;
You can declare the namespace as default, but in this case you must wildcard the outer elements, as they are not part of this namespace:
--Use a default namespace
WITH XMLNAMESPACES(DEFAULT 'https://test.com/')
SELECT #xml.value(N'(/*:ArrayOfArticle/*:Article/ScriptId/text())[1]',N'int') AS ScriptId
,#xml.value(N'(/*:ArrayOfArticle/*:Article/Title/text())[1]',N'nvarchar(max)') AS Title
,#xml.value(N'(/*:ArrayOfArticle/*:Article/Mediatype/text())[1]',N'nvarchar(max)') AS Mediatype;
The recommended approach is to bind the inner namespace to a prefix and use this
--Recommended
WITH XMLNAMESPACES('https://test.com/' AS ns)
SELECT #xml.value(N'(/ArrayOfArticle/Article/ns:ScriptId/text())[1]',N'int') AS ScriptId
,#xml.value(N'(/ArrayOfArticle/Article/ns:Title/text())[1]',N'nvarchar(max)') AS Title
,#xml.value(N'(/ArrayOfArticle/Article/ns:Mediatype/text())[1]',N'nvarchar(max)') AS Mediatype;
If your <ArrayOfArticles> contains more than one <Article> you can use .nodes() to get alle of them as derived table. In this case the query is
WITH XMLNAMESPACES('https://test.com/' AS ns)
SELECT art.value(N'(ns:ScriptId/text())[1]',N'int') AS Recommended
,art.value(N'(ns:Title/text())[1]',N'nvarchar(max)') AS Title
,art.value(N'(ns:Mediatype/text())[1]',N'nvarchar(max)') AS Mediatype
FROM #xml.nodes(N'/ArrayOfArticle/Article') AS A(art);
your XML contains namespaces, I'd use xquery in order to extract the data from your XML
UPDATE with additional elements extract
DECLARE #xml XML;
SET #xml
= '<ArrayOfArticle xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Article>
<ScriptId xmlns="https://test.com/">5135399</ScriptId>
<Title xmlns="https://test.com/">Stocks divided into two corners</Title>
<Mediatype xmlns="https://test.com/">News papeer</Mediatype>
<Abstract xmlns="https://test.com/">Foreign capital doubled this year.</Abstract>
<ScriptDate xmlns="https://test.com/">2017-12-30T00:00:00</ScriptDate>
<ScriptTypeId xmlns="https://test.com/">1</ScriptTypeId>
<ScriptType xmlns="https://test.com/">News general</ScriptType>
<Media xmlns="https://test.com/">Times</Media>
<ArticleUrl xmlns="https://test.com/">http://test.com</ArticleUrl>
<AnalysisResult xmlns="https://test.com/">
<Analysis>
<Regno>111</Regno>
<Name>New York Times</Name>
<Result>1</Result>
<ResultName>Positive</ResultName>
</Analysis>
<Analysis>
<Regno>222</Regno>
<Name>Washington Post</Name>
<Result>1</Result>
<ResultName>Negative</ResultName>
</Analysis>
</AnalysisResult>
<FacebookStats xmlns="https://test.com/">
<ShareCount xsi:nil="true" />
<LikeCount xsi:nil="true" />
<CommentCount xsi:nil="true" />
<TotalCount xsi:nil="true" />
</FacebookStats>
<MediaScore xmlns="https://test.com/">
<MediaScore>
<Regno>111</Regno>
<CompanyName>New York Times</CompanyName>
<MediaScoreID>2</MediaScoreID>
<Name>Neither</Name>
</MediaScore>
<MediaScore>
<Regno>222</Regno>
<CompanyName>Washington Post</CompanyName>
<MediaScoreID>2</MediaScoreID>
<Name>Neither</Name>
</MediaScore>
</MediaScore>
<Page xmlns="https://test.com/">26</Page>
<ProgramId xmlns="https://test.com/">0</ProgramId>
<ProgramTime xmlns="https://test.com/" xsi:nil="true" />
<ProgramLength xmlns="https://test.com/">0</ProgramLength>
<ProgramOrder xmlns="https://test.com/">0</ProgramOrder>
</Article>
</ArrayOfArticle>'
DECLARE #T TABLE (XmlCol XML)
INSERT INTO #T
SELECT #xml
;WITH XMLNAMESPACES ('https://test.com/' as p1)
SELECT z.t.value ('../../p1:ScriptId[1]',' varchar(100)') ScriptId,
z.t.value ('../../p1:Title[1]',' varchar(100)') Title,
z.t.value ('../../p1:Mediatype[1]',' varchar(100)') Mediatype,
z.t.value ('p1:CompanyName[1]', 'varchar(100)') CompanyName
FROM #T t
CROSS APPLY XmlCol.nodes ('/ArrayOfArticle/Article/p1:MediaScore/p1:MediaScore') z(t)
DECLARE #y INT
EXEC sp_xml_preparedocument #y OUTPUT, #xml,
'<ns xmlns:x="https://test.com/"/>'
SELECT *
FROM
OPENXML(#y, '/ArrayOfArticle/Article', 2)
WITH
(
[ScriptId] VARCHAR(20) 'x:ScriptId', --<< and so on
[Title] VARCHAR(30),
Mediatype VARCHAR(30)
)
EXEC sp_xml_removedocument #y --<< lost in your code

Extracting data from XML Nodes

I am testing this simple script and just wondering why it didn't work.
DECLARE #myDoc xml
DECLARE #ProdID int
SET #myDoc =
'<Datatype xmlns="V8_0_1" id="113" name="PF_CleaningMode" hdl="47/4/SB8_3-910-8243-19/0/113" odobjid="915">
<Datatypevalue id="2" name="Intermittent" />
<Datatypevalue id="1" name="Continuous" />
<Datatypevalue id="0" name="Off" />
</Datatype>'
SET #ProdID = #myDoc.value('(Datatype/#id)[1]', 'int' )
SELECT #ProdID
Hope you can assist. Thanks.
You're just simply ignoring the default XML namespace that's defined on your <Datatype> node:
<Datatype xmlns="V8_0_1" id="113" name="PF_CleaningMode"
**************
You need to include that in your query!
Like this:
;WITH XMLNAMESPACES(DEFAULT 'V8_0_1')
SELECT #myDoc.value('(Datatype/#id)[1]', 'INT')
and you'll get the expected output of
113

Parse the XML in SQL Server

<ROOT>
<arn>arn001</arn>
<arn>arn002</arn>
</ROOT>
Tried the following code though
SELECT
ARN.value('(//arn/text())[1]','VARCHAR(100)') AS arns --TAG
FROM
#xml.nodes('/ROOT')AS TEMPTABLE(ARN)
It returns only first value
Try this way :
declare #xml xml = '<ROOT>
<arn>arn001</arn>
<arn>arn002</arn>
</ROOT>'
SELECT
X.value('.','VARCHAR(100)') AS arns
FROM
#xml.nodes('/ROOT/arn')AS T(X)
SQL Fiddle

No return value on using OpenXML command in SQL Server 2008

This is my XML parameter that I set to #XMLSave parameter and send to a stored procedure
<ROOT>
<P>
<ID>123456789</ID>
<Name>admin</Name>
</P>
<Group>
<GroupCardID>14</GroupCardID>
</Group>
</ROOT>
and I try to get ID value with this command
EXEC sp_xml_preparedocument #idoc OUTPUT, #XMLSave
but when I select values return no value
select *
from OPENXML (#idoc,'/Root/P',2) With(ID int)
Try this:
DECLARE #XmlParameter XML = '<ROOT>
<P>
<ID>123456789</ID>
<Name>admin</Name>
</P>
<Group>
<GroupCardID>14</GroupCardID>
</Group>
</ROOT>'
SELECT
#XmlParameter.value('(/ROOT/P/ID)[1]', 'int')
I always prefer the native XQuery support over the clunky old OPENXML stuff.....
I Found the answer finally:
OpenXML parameter is case sensitive :
my XML value start with "ROOT" and openxml parameter was "Root"

Pull out Attributes from TSQL XML

I have the following XML:
<help>
<item field="field1" help="helptext" />
<item field="field2" help="helptext" />
<item field="field3" help="helptext" />
</help>
I have this stored in an XML DataType in SQL server and would like to pull out the fields and help text seperately. I.e a results set similar to:
| field1 | helptext |
| field2 | helptext |
| field3 | helptext |
Does anybody know how to achieve this?
Kindest Regards,
Adam
DECLARE #xml AS XML = '<help>
<item field="field1" help="helptext" />
<item field="field2" help="helptext" />
<item field="field3" help="helptext" />
</help>'
select c.value('#field', 'varchar(max)') field, c.value('#help', 'varchar(max)') help
from #xml.nodes('/help/item') T(c);
To achieve the same when xml data is in a table field use:
SELECT c.value('#field', 'varchar(max)') field, c.value('#help', 'varchar(max)') help
FROM MyTable A CROSS APPLY A.MyXmlField.nodes('/help/item') T(c)
Where MyTable is the table with field MyXmlField containing xml data.
See if following helps:
DECLARE #processedXmlDoc int
DECLARE #inputDoc varchar(1000)
EXEC sp_xml_preparedocument #processedXmlDoc OUTPUT, #inputDoc
SELECT *
FROM OPENXML (#processedXmlDoc , '/help/item',1)
WITH (field varchar(20) 'field',
help varchar(20) 'helpText')
EXECUTE sp_xml_removedocument #idoc
Hope that helps.
Try something like this:
DECLARE #input XML = '<help>
<item field="field1" help="helptext" />
<item field="field2" help="helptext" />
<item field="field3" help="helptext" />
</help>'
SELECT
Field = Item.value('#field', 'varchar(25)'),
HELP = Item.value('#help', 'varchar(50)')
FROM
#input.nodes('/help/item') AS XTbl(Item)