I am using SQL server 2008 and I'm quite new to writing sql. My aim is to export data from a table into xml format to create a CAP xml file that can be used in our website. Currently, I'm just writing some select statements to retrieve data in the correct format. Here is the code:
select (SELECT TOP 5 [Master_Incident_Number] AS incidents
,[Jurisdiction] AS jurisdiction
,[Response_Date] AS Date
FROM [ESCAD_DW_System].[dbo].[CurrentIncidents_V] Incident
FOR XML PATH ('area'), type ) AS Alert for xml path (''),
ROOT ('?xml version = "1.0" encoding = "UTF-8"?')
However, I am getting 'invalid XML identifier' error for '?' symbol. Can anyone help?
You cannot use ROOT to add an xml encoding tag. The only way to do that is to convert the xml output into varchar(max) and prepend with your encoding tag. Keep in mind though that FOR XML output is UTF-16 by default, and therefore your UTF-8 encoding is probably unnecessary. Having said that, here is a simple example that uses a UDF to convert the xml into varchar(max):
create function gimmexml()
returns varchar(max)
as
begin
return (
select a='some', b='xml'
for xml path ('')
)
end
go
select '<?xml version="1.0" encoding="UTF-8"?>'+dbo.gimmexml();
Result:
<?xml version="1.0" encoding="UTF-8"?><a>some</a><b>xml</b>
Further reading:
http://www.devnewsgroups.net/group/microsoft.public.sqlserver.xml/topic60022.aspx
http://msdn.microsoft.com/en-us/library/ms345137%28v=sql.90%29.aspx
Related
I extracted xml file and move it SQL Server.
It now looked like this.
Now I'm trying to convert it to XML data type.
DECLARE #XML AS XML
SELECT #XML = convert(xml,[Column 0],2) FROM TestExtract
But Im getting this error
XML parsing: line 1, character 43, unable to switch the encoding
Maybe I should put all rows into one? then convert it?
Please advise for any options.
Thanks!
I used SSIS, flat file as a source and ole db(SQL server) as destination
Why you haven't imported the XML using XML Source instead of Flat File source?
If the XML file is well structured then you can use an XML Source to import data into SQL Server, there are many example found online:
Importing XML documents using SQL Server Integration Services
Extract Data by Using the XML Source
XML Source
Other approach
You can directly read the XML file from SQL Server using ad hoc queries (OPENROWSET):
Importing and Processing data from XML files into SQL Server tables
Current situation
First of all combine all rows into one value, then try to convert the value.
DECLARE #strXML VARCHAR(MAX)
DECLARE #XML AS XML
SET #strXML = ''
SELECT #strXML = #strXML + [Column 0] FROM Testextract
//SELECT #strXML = #strXML + [Column 0] + CHAR(13) + CHAR(10) FROM Testextract
SET #XML = convert(xml,#strXML,2)
Updated an xml file in order to remove an unnecessary field using
deletexml(xmltype(xxx)).getClobVal()
but the XML returns as one long string instead of a properly formatted XML file with indents and spaces. Any idea what I'm doing wrong here? Thanks
getClobVal, getStringVal are deprecated since oracle 11.2 .instead of these function you have to use xmlserialize.
Example:
select xmlserialize(document xmltype('<a><b><c>xxx</c></b></a>') indent size=2) from dual;
And you will end with clob object containing pretty-print xml.
"properly formatted XML file with indents and spaces"
That might surprise you, but that is properly formatted (well-formed) XML. The XML standard says nothing about whitespace between structural elements, except that it is allowed. It's called "insignificant white-space" for a reason.
If you want to format your XML for human-readability, you must do that yourself. But XML isn't for humans, it's for machines, so there is no reason to have your SQL do such formatting. Use any tool you like that auto-formats XML for human readability if you want to inspect the XML as human.
I use this procedure to make a pretty XML:
PROCEDURE MakePrettyXml(xmlString IN OUT NOCOPY CLOB) IS
xmlDocFragment DBMS_XMLDOM.DOMDOCUMENTFRAGMENT;
xslProc DBMS_XSLPROCESSOR.PROCESSOR;
xsl DBMS_XSLPROCESSOR.STYLESHEET;
xmlStringOut CLOB;
BEGIN
DBMS_LOB.CREATETEMPORARY(xmlStringOut, TRUE);
xslProc := DBMS_XSLPROCESSOR.NEWPROCESSOR;
xsl := DBMS_XSLPROCESSOR.NEWSTYLESHEET(
'<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">'||
'<xsl:output method="xml" indent="yes"/>'||
'<xsl:template match="#*|node( )">'||
'<xsl:copy>'||
'<xsl:apply-templates select="#*|node( )"/>'||
'</xsl:copy>'||
'</xsl:template>'||
'</xsl:stylesheet>', NULL);
xmlDocFragment := DBMS_XSLPROCESSOR.PROCESSXSL(p => xslProc, ss => xsl, cl => xmlString);
DBMS_XMLDOM.WRITETOCLOB(DBMS_XMLDOM.MAKENODE(xmlDocFragment), xmlStringOut);
DBMS_XSLPROCESSOR.FREESTYLESHEET(xsl);
DBMS_XSLPROCESSOR.FREEPROCESSOR(xslProc);
xmlString := xmlStringOut;
DBMS_LOB.FREETEMPORARY(xmlStringOut);
END MakePrettyXml;
But note the output is a CLOB rather than a XMLTYPE, you may need some additional conversions.
i have this XQuery
declare #XML xml
set #XML =
'
<root>
<row1>
<value>1</value>
</row1>
<1row2>
<value>2</value>
</1row2>
</root>
'
select #XML.query('/root/1row2')
i keep on getting an error white trying to select 1row2.
this error
XQuery [query()]: Syntax error near '1', expected a step expression.
is seems that i just keep getting this error when xml node start with a number is there a way to select the said node?
From XML Naming Rules, XML elements must follow these naming rules:
Element names are case-sensitive
Element names must start with a letter or underscore
Element names cannot start with the letters xml (or XML, or Xml, etc)
Element names can contain letters, digits, hyphens, underscores, and
periods
Element names cannot contain spaces
Any name can be used, no words are reserved (except xml).
So, the elements names must start with a letter or underscore. On SQL Server 2016 SP1 your XML is event not a valid and cannot be executed:
You need to either repair your string to be a valid XML or to query the data using some other technique (for example, SQL CLR function to implement regex expression support or splitting the nodes).
Does SSMS - SQL Server 2014 have an option to automatically indent XML text?
I save XML text in a column (nvarchar(max)) to analyze the input of an application.
Usually the result of my queries are set to grid and I copy and paste the result into the query editor to read it.
This is what I get:
<?xml version="1.0"?><farm-confirm source="orders.company.com"><Detail><item_keyid>3207890</item_keyid><item_code>50002035</item_code></Detail></farm-confirm>
This is what I would like:
<?xml version="1.0"?>
<farm-confirm source="orders.company.com">
<Detail>
<item_keyid>3207890</item_keyid>
<item_code>50002035</item_code>
</Detail>
</farm-confirm>
Thanks
Given the XML is well formed, the easies was to do this:
DECLARE #xml XML=N'Put your XML here';
SELECT #xml;
(Output to Grid-View)
And now just click on the XML. The XML-Viewer will present it formatted and indented.
Or take one of the free online XML prettyzizers.
Just google for online pretty xml formatter
update
If you get the XML (which is - in your case - a string actually) from a query, you might just wrap the column with CAST(MyColumn AS XML). This will offer you the XML-Viewer immediately...
I have some odd data which i am putting in an xml file (ansi/utf-8).
I am having trouble with symbols that web browsers cannot parse.
Here is an example of the troublesome data:
ColumnA
no sería tan divertido
Here is my select Statement:
SELECT
'test' as 'node/#attribute'
,Column_A as 'node'
FROM
TableA
for xml PATH('record'), ROOT('log')
And here is the error i get when FF or IE try to open the document:
XML Parsing Error: not well-formed
and then it will subsequently point me to the above data.
Is there a way normalize all text in ColumnA to avoid this issue?
Thank you.
With None encoding everything is just fine
The query
SELECT TOP 1
'test' as 'node/#attribute'
,N'no sería tan divertido' as 'node'
for xml PATH('record'), ROOT('log')
The xml
<log>
<record>
<node attribute="test">no sería tan divertido</node>
</record>
</log>
IE