Solved

Convert TEXT field to XML field

Posted on 2008-06-19
5
1,234 Views
Last Modified: 2012-05-05
I have a table with a TEXT field containing a xml. I want to convert the contents of that field to a XML field. I used CAST(tmp_text AS XML). The problem is, some of the fields do not contain a valid xml so I need to filter these out. I need something like a ISXML function like ISDATE and ISNUMERIC. How can I do this?  
0
Comment
Question by:Lexie
  • 3
  • 2
5 Comments
 
LVL 51

Expert Comment

by:Mark Wills
ID: 21824900
Maybe check for the existance of < and > and </

could create a function...


create function uXML (@input varchar(max))
returns bit
as
begin
  if charindex(@input,'<') < 1 return 0
  if charindex(@input,'>') < 1 return 0
  if charindex(@input,'</') < 1 return 0
return 1
end

declare @str varchar(2000)
set @str = 'wehwjc c f1234 < dhsa'

if dbo.uxml(@str) > 0 print 'True' else print 'false'
0
 
LVL 3

Author Comment

by:Lexie
ID: 21828985
The tags are all right, the problem turns out to be the character & like this in the TEXT field:
<accommodation name="Hotel Houda Golf & Beach Club"  cms-id="3871" >
 
I can filter on the & like this tra_text NOT LIKE '%&%' but this would also filter these:
<message>&gt;&gt; Allotment released - Booking on request only &lt;&lt;</message>

So I am looking for a way to validate the XML on all aspects, not only some tags.
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 125 total points
ID: 21829043
Well there are about 5 or so "standard" xml replacement characters in the XML standard, but could be a whole lot more...

can check for  &quot; &lt; &gt; &amp; &apos;    and can also check for '% & %' ....  BUT, by the sounds of it, whatever is generating the xml output is not doing the right thing with replacement characters, so could possibly assume that any instance of & is not part of the standard...

The problem doing a validation check is that it also needs to consider the entire document structure as well as the little bits. It can be very hard to do... First really need a schema so you have a basis to compare against in terms of message structrue, then there is the syntax check etc etc...

For example, if you were to double click on the XML file containing the above sample, Internet Explorer will likely be the viewer - and it is a program that is really at home with XML - but - pounds to pence the error meesage will manifest itself elsewhere and you have to track back to find the real error. That best exemplifies the kind of challenge that can lay ahead. However, if the structure is pretty simple, extremely reliable in terms of tag content and hierarchy, then can probably knock something up...

0
 
LVL 3

Author Closing Comment

by:Lexie
ID: 31468719
Too bad there is not function that returns a boolean like ISDATE and ISNUMERIC.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 21829184
Well, at least 2008 is offering LAX validation for those optional elements in a schema - small steps, but no "isXML" might try to write one... Let me know if you need anything more.

Cheers,
Mark Wills
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now