Earlier,In TSQL we have an XML column to store the html data with xml serialization.
But now we think to keep the html content in CDATA region.
How can I convert the existing xml serialized content to the corresponding html content?
e.g. XML serialized column data : <Node Txt="<b>bold text</b>" />
Expected corresponding transform : <Node><![CDATA[<b>bold text</b>]]></Node>
The above transformation is expected to be carried over by sql script.
I think of a solution to replace all those 5 xml special chars corresponding replacement characters (&,<,>,",etc.). But I dont think string manipulation may work in xml to html transformation.
Any cleaner way or idea to transform those existing xml to html data?
Maybe use the PHP function htmlspecialchars to translate it. If it's a one time thing, this shouldn't be too much trouble for you.
If not, you could code something up using SQL string functions. http://msdn.microsoft.com/en-us/library/ms186862.aspx
Related
I want to include a line (simple text) in ForXMLPath query as
<Cat>
but I am having difficulties.
When I try it brings in weird characters with it.
Please help.
Thanks.
select
'<Cat>'
I expect this
<Cat>
but it displays below
<Cat>
I must admit, that your question is not clear...
XML is not just some text with fancy extras, but a very strictly organised text based container for data.
A simple SELECT '<Cat>' would never return as <Cat> without a FOR XML somewhere in your query. So please show us a (reduced!) example of your full query and the expected output, best provided as MCVE (a stand-alone sample with DDL, sample data, own attempt and expected output).
Just some general remarks:
If you want to place <Cat> within your XML the whole output will be broken XML. This opening tag demands for a closing </Cat> (or - alternatively - a self-closing <Cat />)
Assumably you try to add out-written tags to your XML as you'd do it in XSLT, JS, ASP.Net or any other XML/HTML producing approach.
Assumably your solution will be a FOR XML PATH() approach without the need of an out-written tag within your XML.
Just to give you an idea:
SELECT 'test' AS [SomeElement] FOR XML PATH('SomeRowTag'),ROOT('SomeRootTag');
prouces this XML
<SomeRootTag>
<SomeRowTag>
<SomeElement>test</SomeElement>
</SomeRowTag>
</SomeRootTag>
If you want to add a <Cat> element you could use an XPath like here
SELECT 'test' AS [Cat/SomeElement] --<-- You can add nest-levels here!
FOR XML PATH('SomeRowTag'),ROOT('SomeRootTag');
The result
<SomeRootTag>
<SomeRowTag>
<Cat>
<SomeElement>test</SomeElement>
</Cat>
</SomeRowTag>
</SomeRootTag>
Can we call an external file (xml, Excel or text file) from XSL?
How do I call SQL Server stored procedures through XSL?
All I am trying to figure out is a way to do mappings from source XML to target XML (let's say if the value of mode vehicle is X in the source, I need to translate it to Y in translate XML based on the mapping stored either in an external file or through a SQL Server stored procedure).
XSLT 1 has the document function (https://www.w3.org/TR/xslt-10/#document) to pull in additional XML documents. Beyond that in XSLT 1 you need to look at extension scripts or elements to deal with other sources, such ways depend on the XSLT processor and/or the programming language or platform (e.g. Java or .NET) it is implemented in.
XSLT 2 in addition to the document function has the doc function for dealing with XML input, it has the unparsed-text function (https://www.w3.org/TR/xslt20/#function-unparsed-text) for text formats and collection (https://www.w3.org/TR/xquery-operators/#func-collection) for input collections. As for SQL, as Saxon 9 on Java is probably the XSLT 2 processor used most, it has in its commercial editions an SQL extension http://saxonica.com/html/documentation/sql-extension/.
XSLT 3 adds support for JSON with functions like parse-json (https://www.w3.org/TR/xpath-functions/#func-parse-json) or json-doc (https://www.w3.org/TR/xpath-functions/#func-json-doc) or json-to-xml (https://www.w3.org/TR/xpath-functions/#func-json-to-xml).
In general I wonder why you need XSLT plus a custom mapping format as in my view template based XSLT in the form
<xsl:template match="foo">
<bar>
<xsl:apply-templates/>
</bar>
</xsl:template>
is the way to declare and implement the mapping. However, if you have a custom mapping, then in all versions of XSLT you can use it create XSLT with XSLT and then run the created XSLT to execute the mapping.
When I try to store a XML in SQL than have an empty Element, SQL just change it and store it with only one tag for the element.
For Example the XML to store is:
<ROOT>
<FIRSTNAME>ROGER</FIRSTNAME>
<MIDDLENAME></MIDDLENAME>
</ROOT>
Then Sql stored it like
<ROOT>
<FIRSTNAME>ROGER</FIRSTNAME>
<MIDDLENAME />
</ROOT>
The sql update is just very simple:
UPDATE
SESIONESREPORTES
SET
SER_PARAMETROS = '
<ROOT>
<FIRSTNAME>ROGER</FIRSTNAME>
<MIDDLENAME></MIDDLENAME>
</ROOT>'
WHERE SER_ID=7
I need like this because I have some query that fails when a element is empty, you can see it here..
Merging many rows in a single
I don't think you can, looking at the following link:
XML Data Type and Columns
According to this (XML Storage Options Section):
The data is stored in an internal representation that preserves the
XML content of the data. This internal representation includes
information about the containment hierarchy, document order, and
element and attribute values. Specifically, the InfoSet content of the
XML data is preserved. For more information about InfoSet, visit
http://www.w3.org/TR/xml-infoset. The InfoSet content may not be an
identical copy of the text XML, because the following information is
not retained: insignificant white spaces, order of attributes,
namespace prefixes, and XML declaration.
So the internal storage will strip out all parts it deems unnecessary, the document goes on to state that if you need an exact copy of the XML document and not just the content, you should use either [n]varchar(max) or varbinary(max)
<MIDDLENAME></MIDDLENAME>
and
<MIDDLENAME/>
are equivalent; any XML parser will treat them identically - as an empty element. If your query fails on an empty element, it will fail on either of them. You'll need to either rewrite your query to handle empty elements, put some content in the <MIDDLENAME> element, or omit the element entirely (if your query can handle it's absence.)
T-SQL XML value loses new line formats
I have XML file loaded into SQL server. I query this file to extract the nodes with value.
The problem is the new line characters are lost while selection. How to retain formatting so that when I display the text on the web, it not appear messy without line breaks.
See text and screenshots for details
T-SQL code:
declare #Text xml ;
set #Text= '<?xml version="1.0" encoding="utf-8"?>
<topic>
<L1>
<Subject>Subject text</Subject>
<Details>
Story Details are
This is paragraph
Text after After two line breaks
Text after After two line breaks
</Details>
</L1>
</topic>'
;with t as (select #Text [xmlcolumn])
--select * from t
SELECT x.a.value('(Subject)[1]','nvarchar(max)') as [Subject]
, x.a.value('(Details)[1]','nvarchar(max)') as [Details]
FROM t
cross apply
t.xmlcolumn.nodes('//L1') x(a)
Update: I misread your question - the problem with the newlines is purely in SQL Server Management Studio - it cannot represent those newlines. When you read your XML from an application in C# or VB.NET, those newlines will still be there - trust me.
But this original answer might also be relevant in other cases - you need to be aware that SQL Server is not storing your XML "as is" - it parses and converts it. So when you ask to get it back, it might look slightly different, but it's still the same XML functionally.
Yes, this is normal, expected behavior.
SQL Server stores your XML in a tokenized format - e.g. it doesn't store the actual, textual representation of your XML, but it parses and tokenizes your XML into XML fragments that are then stores inside this XML datatype.
Therefore, when you query it again, you'll get back a semantically correct and identical representation - but there's a possibility that certain textual representations are different.
E.g. when you pass in an empty XML element something like this:
<MyEmptyElement></MyEmptyElement>
you'll get back the "short" form of that when you retrieve the XML from SQL Server again:
<MyEmptyElement />
This is not the exact same text - but it's 100% the same XML from a semantic perspective.
As far as I know, you cannot influence this behavior in any way - you'll just have to live with it.
I am generating XML from SQL Server 2005 using a SELECT statement with XQuery syntax.
Is there a way to generate end tags for empty elements? Basically the xml output generated from this sql statement feeds into a "legacy" c# xml parser that doesn't like minimized tag elements! Otherwise everything works fine.
select
-- (this generates empty xml element which throws out the parser)
main.sub.query('schoolname').value('.','varchar(50)') "newparent/newchild/newschoolname"
from
#xml.nodes('/parent/child') AS main(sub)
for xml path(''), type)
Thanks
Use an XSLT processor to transform the empty tags to paired tags, or remove them as in the following questions:
XSLT stylesheet replaces self-closing tags with empty paired tags
Removing empty tags from XML via XSLT