Include text in ForXMLPath query in SQL Server - sql-server

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>

Related

RegEx for replacing the tag elements in HTML for SQL Server

In a SQL Server database table, I have a column like this:
<p>Radio and television.</p><p>very popular in the world today.</p><p>Millions of people watch TV. </p><p>That’s because a radio is very small <span_style=":_black;">98.2%</span></p><p>and it‘s easy to carry. <span_style=":_black;">haha100%</span></p>
I want to delete the <p> and </p> and <span_style=":_black;"> and </span> and all tag elements in HTML.
The text I ultimately want is like this:
Radio and television.very popular in the world today.Millions of people watch TV.That’s because a radio is very small 98.2% and it‘s easy to carry.haha100%
I want to do it with regular expressions. But, I can't find a regular expression to solve this problem.
What should I do?
I think you don't need a regular expression here, try to use the following:
DECLARE #html nvarchar(MAX) = N'<p>Radio and television.</p><p>very popular in the world today.</p><p>Millions of people watch TV. </p><p>That’s because a radio is very small <span_style=":_black;">98.2%</span></p><p>and it‘s easy to carry. <span_style=":_black;">haha100%</span></p>';
SET #html=REPLACE(#html,'span_style','span style') -- wrong tag
DECLARE #xml xml = #html
-- demo with variable
SELECT t.c.value('.','varchar(max)') AllText
FROM #xml.nodes('/') t(c)
-- demo with query
SELECT (SELECT t.c.value('.','varchar(max)') FROM q.xml_col.nodes('/') t(c)) AllText
FROM
(
-- your query with a xml-column is here
SELECT CAST(#html AS xml) xml_col -- row 1
UNION ALL
SELECT CAST(#html AS xml) xml_col -- row 2
) q
This RegEx might help you to do so:
((\<)[\w\/\=\x22\x27\:-;]+(>))
You only need to add any other chars that you may have in, such as space:
[\w\/\=\x22\x27\:-;]
You can simply replace $1 with an empty string, using your codes.
You might also consider other application-based requirement and language specific metachar escaping that you might have.
You can also simplify this regex, if you wish:
About special chars, you might check for unicode/ASCII based on your desired language.
You can simply add it to the RegEx. For example, if you have special quotation marks, you can update it similar to this RegEx:
((\<)([\w\/\=\"\'\‘\:\’\-;\s]+)(>))
This RegEx is pretty simple to understand:
It has a simple left boundary, <, in a capturing group just to be safe
((\<)
It has a simple right boundary: >, in a capturing group just to be safe
(>))
It has a middle capturing group, where all your chars should be included:
([\w\/\=\"\'\‘\:\’\-;\s]+)
Then, it wraps up these three capturing groups in a group, which is not really necessary to do so, it is just to be safe, adding an extra boundary.
I do not know about SQL Servers, but this
post
might help you to maybe design a query to do so.

SQL Server XML Value formatting newline

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.

How to prevent minimized empty xml tags using XQuery from SQL Server 2005

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

Decoding the xml to html content from t sql xml column

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

Read value of XML attribute in SQL Server 2005

I have one table contains field named source with varchar(max).
That field has following value
<OutPatientMedication
DateFormat="MM-dd-yyyy"
MedicationName="lisinopril 10 mg oral tablet"
Instructions="2 cap(s) orally once a day "
Status="Active"
Quantity="0"
Refills="0"
PrescriptionType="E">
</OutPatientMedication>
Now I want to fetch value of Instructions attribute.
How can I fetch value?
Prompt reply will be appreciated.
Thanks,
Dhruval Shah
Try something like this:
SELECT
CAST(Source AS XML).value('(/OutPatientMedication/#Instructions)[1]', 'varchar(200)')
FROM
dbo.YourTable
WHERE
(condition)
That should give you the desired value.
If you really have only XML in that column, I would strongly recommend making it of type XML in the database! Makes your life a lot easier, and save on disk space, too.

Resources