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'm new to Mule, so bear with me. I have the following CSV file that I receive:
Company1,2,123 Street,Winchester,UK
"000010","CHRISTINE","I","HAAS","A00","3978","1995-01-01","PRES",18,"F","1963-08-24",152750.00
"000020","MICHAEL","L","THOMPSON","B01","3476","2003-10-10","MANAGER",18,"M","1978-02-02",94250.00
The first line, header, contains company info plus the number of records (number of employees) in CSV file (second parm in the header).
Now I need to convert it to the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<tns:employeedata xmlns:tns="http://coxb.test.legstar.com/payrollemployee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://coxb.test.legstar.com/payrollemployee PayrollEmployee.xsd ">
<tns:employeecount>2</tns:employeecount>
<tns:employeelist>
<tns:employees>
<tns:employeenumber>000010</tns:employeenumber>
<tns:firstname>CHRISTINE</tns:firstname>
<tns:middleinitial>I</tns:middleinitial>
<tns:surname>HAAS</tns:surname>
<tns:department>A00</tns:department>
</tns:employees>
<tns:employees>
<tns:employeenumber>000020</tns:employeenumber>
<tns:firstname>MICHAEL</tns:firstname>
<tns:middleinitial>L</tns:middleinitial>
<tns:surname>THOMPSON</tns:surname>
<tns:department>B01</tns:department>
</tns:employees>
</tns:employeelist>
</tns:employeedata>
I could easily transform this file without the first line (header). My problem is how to process the header and extract/transform "employeecount".
Any help will be greatly appreciated.
The easiest way to do this is to use DataMapper. Set the input to CSV (using a sample CSV) and the output to XML (using your XSD or a sample XML).
Once you're in the mapping view, click on your employeecount field. You'll see an area where you can enter an expression. There is a non-documented parameter $in.0.__id which you can use which will contain the record count. Note that this will only work for CSV files.
Regarding how to skip the first line, DataMapper does this by default.
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
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
I'm running a query from SQL Server Management Studio 2005 that has an HTML file stored as a a string, e.g.:
SELECT html
FROM table
This query displays in the "Results" window nicely, i.e., each row contains a string of the whole HTML file, with one record per row.
However, when I use the "Results to file" option, it exports it as an unusable CSV with line breaks in the CSV occurring wherever line breaks occurred in the field (i.e., in the HTML), rather than one per row as needed. I tried experimenting with the Query>Query Options for both the "Grid" and "Text" results, to no avail. The fields exported to the CSV do not appear to be enclosed within quotes.
Some ideas:
Might it be possible to append
quotation marks w/ the SQL?
Is there some T-SQL equivalent to the
WITH CSV HEADER commands that are
possible in other dialects?
I don't see where you will have much success exporting html to csv - it's really not what csv is meant for. You would be better off using an xml format, where the html code can be enclosed in a cdata element.
That said, you could try using the Replace function to remove the line breaks, and you could manually add the quotes - something like this:
select '"' + replace (replace (html, char(10), ''), char(13), '') + '"'
If your html value could have double quotes in it, you would need to escape those.
If you are using Visual Studio, Server Explorer is an alternative solution. You can correctly copy & paste the results from its grid.
According to the closest thing to a standard we have, a correctly formatted CSV-file should quote fields containing either the separator (most often ; or ,) or a linebreak.
SQL Server Management Studio can do that, but amazingly it doesn't have this option enabled by default. To enable, go to Tools → Options → Query Results → Results to Grid and check "Quote strings containing list separators when saving .csv results"
I know how old this is, but I found it and others might, as well. You might want to take Martijn van Hoof's answer one step further and remove possible tabs (char(9)) in addition to carriage return(13) and line feed(10).
SELECT REPLACE(REPLACE(REPLACE(mycolumn, CHAR(9), ''), CHAR(10), ''), CHAR(13), '') as 'mycolumn' FROM mytable
column with line breaks: "mycolumn"
SELECT REPLACE(REPLACE(mycolumn, CHAR(13), ''), CHAR(10), '') as `mycolumn` FROM mytable
This replaces the linebreaks.
i know this is very old question and has been answered now but this should help as well:
SELECT html
From table
For Xml Auto, Elements, Root('doc')
it should spit out an xml and then you can import that xml into excel
found this useful but I was still hitting problems as my field was type text so I cast the text as varchar(8000) and above replace works like a charm
REPLACE(REPLACE(CAST([TEXT FIELD] AS VARCHAR(8000)), CHAR(10), ' '), CHAR(13), ' ') AS 'Field Name',
SQL Server Import and Export Data tool, FTW!
Start -> Programs -> Microsoft SQL Server -> Import and Export Data
Sample query:
select *
from (
select 'Row 1' as [row], 'Commas, commas everywhere,' as [commas], 'line 1
line 2
line 3' as [linebreaks]
union all
select 'Row 2' as [row], 'Nor any drop to drink,' as [commas], 'line 4
line 5
line 6' as [data]
) a
CSV output:
"row","commas","linebreaks"
"Row 1","Commas, commas everywhere,","line 1
line 2
line 3"
"Row 2","Nor any drop to drink,","line 4
line 5
line 6"
Disclaimer: You may have to get creative with double-quotes in the data. Good luck!
I got around this limitation by creating an Access database, using the "Link to the data source by creating a linked table" feature, opening the "linked" table, then copy/paste to Excel from there. Excel can save the CSV data as needed. You should be able to connect directly from Excel too, but the "linked table" feature in Access let me set up several tables at once pretty quickly.