Solved

Convert TEXT field to XML field

Posted on 2008-06-19
5
1,232 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In this article I will describe the Copy Database Wizard 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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

12 Experts available now in Live!

Get 1:1 Help Now