?
Solved

Convert TEXT field to XML field

Posted on 2008-06-19
5
Medium Priority
?
1,246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 375 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 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