?
Solved

Convert TEXT field to XML field

Posted on 2008-06-19
5
Medium Priority
?
1,244 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

762 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