Question : sp_xml_preparedocument issue

Hi Experts,
Bit involved this, here goes.

My investigations into an issue started with the error being returned from a call to sp_xml_preparedocument:
XML parsing error: An Invalid character was found in text content.

I read up on this, and duly changed the @xmlfile parameter being passed to the call:
exec sp_xml_preparedocument @idoc output, @xmlfile

..to an ntext column.
This worked ok, until I realised that the client code (which is going to be a pain to change at this stage), passes the encoding header:

So, this meant, I now received the error:
XML parsing error: Switch from current encoding to specified encoding not supported.

If I did a replace of this encoding within QA and called my proc, using:
encoding="windows-1251"
...then all was good, so I figured I could just run this find/replace within my stored proc on the ntext XML before passing it to the sp_xml_preparedocument statement.

So now it gets really interesting/spikey.

I'm trying to use UPDATETEXT to amend the ntext field, which I can successfully do, but only if I send the @xmlfile contents into a temporary table. However, the xp_xml_preparedocument statement won't accept a column, it insists on a variable.

Obviously SQL has succeeded in closing off all my useful options, by returning:
The assignment operator operation cannot take a ntext data type as an argument.
...when I try and set the amended data in the table, back to the variable.

Please help!

Below is the excerpt from the proc:
*******************************************************
CREATE PROCEDURE docsadm.ao_sp_set_subscribedmatters
@user_id varchar(20),
@xmlfile ntext,
@confirm_remove_all bit=0,
@debug bit=0
AS

--environment
set nocount on

--temp tables

--create temporary table to hold xmlfile, so that updatetext functions
--UPDATETEXT does not appear to work against ntext variables
if object_id('tempdb.dbo.#xmlfile') is not null drop table #xmlfile
create table #xmlfile
(xmlfile ntext)

--declarations
declare @idoc int
declare @nfindtxt nvarchar(200)
declare @nreplacetxt nvarchar(200)
declare @txtlen int
declare @ptr binary(16)
declare @pos int

--Insert XML text into Table
insert #xmlfile select @xmlfile

--Replace encoding header in XML to fix entended character issues
select @nfindtxt=N'encoding="utf-8"'
select @nreplacetxt=N'encoding="windows-1251"'
select @txtlen=len(@nfindtxt)
select @ptr=textptr(xmlfile) from #xmlfile
select @pos=charindex(@nfindtxt, @xmlfile)-1

updatetext #xmlfile.xmlfile @ptr @pos @txtlen @nreplacetxt

--select xmlfile from #xmlfile

--select @xmlfile=(select xmlfile from #xmlfile)

--create an internal representation of the XML doc
--exec sp_xml_preparedocument @idoc output, @xmlfile
exec sp_xml_preparedocument @idoc output, @xmlfile

go

Answer : sp_xml_preparedocument issue

There is a description of a method here:
    How can I parse XML from a text column using sp_xml_preparedocument?
    http://www.sqlxml.org/faqs.aspx?faq=42

It looks like it boils down to breaking up the TEXT value from the temp table into a set on VARCHAR values and then building and executing a dynamic command string.

It is worthwhile reading the replies/Feedback to that as there are a number of good suggestions/improvements there.

-- Dan
Random Solutions  
 
programming4us programming4us