Link to home
Start Free TrialLog in
Avatar of khinvra
khinvra

asked on

Reading XML File Created by ADO in SQL Server using OpenXML

Hi,

following is a query which runs perfectly

DECLARE @idoc int
DECLARE @doc varchar(1000)

SET @doc ='
<rsdata>
<zrow Id="1860" Name="CDC CARBOLINE ">
  <Transactions TransactionId="27" PartyId="1860" TransactionDate="2004-04-01T000000" TransactionSerial="04-000002">
  <TransactionDetails TransactionId="27" ProductName="PAINT\EPOXY\CARBOZINC-12\CDC\GREY WITH HARDNER" />
  <TransactionDetails TransactionId="27" ProductName="THINNER\NO-#26\CDC\~N.S~" />
  <TransactionCharges TransactionId="27" ChargeId="25" ChargeValue="0" />
  <TransactionCharges TransactionId="27" ChargeId="27" ChargeValue="0" />
  <TransactionCharges TransactionId="27" ChargeId="26" ChargeValue="0" />
  <TransactionCharges TransactionId="27" ChargeId="28" ChargeValue="0" />
  <TransactionCharges TransactionId="27" ChargeId="24" ChargeValue="0" />
  </Transactions>
</zrow>
</rsdata>
'
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT    *
FROM       OPENXML (@idoc, '/rsdata/zrow',1)
            WITH ([Id]  varchar(10),
                  Name varchar(20))

and gives result like this

Id         Name                
---------- --------------------
1860       CDC CARBOLINE

(1 row(s) affected)


The problem is Above xml is created by an ADO command using a shape file which orignally looks like this

DECLARE @idoc int
DECLARE @doc varchar(1000)

SET @doc ='
<rs:data>
<z:row Id="1860" Name="CDC CARBOLINE ">
<Transactions TransactionId="27" PartyId="1860" TransactionDate="2004-04-01T00:00:00" TransactionSerial="04-000002">
  <TransactionDetails TransactionId="27" ProductName="PAINT\EPOXY\CARBOZINC-12\CDC\GREY WITH HARDNER" />
  <TransactionDetails TransactionId="27" ProductName="THINNER\NO-#26\CDC\~N.S~" />
  <TransactionCharges TransactionId="27" ChargeId="25" ChargeValue="0" />
  <TransactionCharges TransactionId="27" ChargeId="27" ChargeValue="0" />
  <TransactionCharges TransactionId="27" ChargeId="26" ChargeValue="0" />
  <TransactionCharges TransactionId="27" ChargeId="28" ChargeValue="0" />
  <TransactionCharges TransactionId="27" ChargeId="24" ChargeValue="0" />
  </Transactions>
  </z:row>
  </rs:data>
'
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT    *
FROM       OPENXML (@idoc, '/rs:data/z:row',1)
            WITH ([Id]  varchar(10),
                  Name varchar(20))
-------------------------------------------------------
If I run above query it gives following error :-

Server: Msg 6603, Level 16, State 1, Procedure sp_xml_preparedocument, Line 20
XML parsing error: Reference to undeclared namespace prefix: 'rs'.
---------------------------------------------------------
and if I replace ":" by "" and then  the query works fine
---------------------------------------------------------
I do not want to replace the ":" as I cannot do that in my production. so I am looking for a solution where I can use OpenXML without replacing ":"

Rahul
Avatar of patrikt
patrikt
Flag of Czechia image

There has to be namespace declaration. You should modify xml as this:

'<root xmlns:rs="urn:myRS" xmlns:z="urn:myZ">'+@doc+'</root>'

and OPENXML acordingly OPENXML (@idoc, 'root/rs:data/z:row',1)

I thing that this can solve problem even if you have no power to modify source xml becouse this is only addition before and after.

Patrik
Avatar of khinvra
khinvra

ASKER

the same script runs without the

'<root xmlns:rs="urn:myRS" xmlns:z="urn:myZ">'+@doc+'</root>'


only we have to do is replace ":" with ""... so I think the problem is  not with namespace declaration. but with the ":"

Rahul
Yes. When you remove : you delete reference to that namespace so you solve problem. But I supoose that you cannot modify xml that way in production.

In my solution you don't need to modify xml. You only add namespace definition on start of document which is simplier than replacing. As soon as namespaces are defined you can use rs: and z: prefixes and xml will be valid.

In general your xml is not correct so you have to do something to repare it. Namespace prefix could not be there without definition.

Patrik
Avatar of khinvra

ASKER

following is changed as per your suggestion :-


DECLARE @idoc int
DECLARE @doc varchar(1000)

SET @doc ='
<rs:data>
<z:row Id="1860" Name="CDC CARBOLINE ">
<Transactions TransactionId="27" PartyId="1860" TransactionDate="2004-04-01T00:00:00" TransactionSerial="04-000002">
  <TransactionDetails TransactionId="27" ProductName="PAINT\EPOXY\CARBOZINC-12\CDC\GREY WITH HARDNER" />
  <TransactionDetails TransactionId="27" ProductName="THINNER\NO-#26\CDC\~N.S~" />
  <TransactionCharges TransactionId="27" ChargeId="25" ChargeValue="0" />
  <TransactionCharges TransactionId="27" ChargeId="27" ChargeValue="0" />
  <TransactionCharges TransactionId="27" ChargeId="26" ChargeValue="0" />
  <TransactionCharges TransactionId="27" ChargeId="28" ChargeValue="0" />
  <TransactionCharges TransactionId="27" ChargeId="24" ChargeValue="0" />
  </Transactions>
  </z:row>
  </rs:data>
'

set @Doc = '<root xmlns:rs="urn:myRS" xmlns:z="urn:myZ">'+@doc+'</root>'

--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT    *
FROM              OPENXML (@idoc, 'root/rs:data/z:row',1)
            WITH ([Id]  varchar(10),
                  Name varchar(20))

-----
Still not working  gives same problem.

Server: Msg 6603, Level 16, State 1, Line 25
XML parsing error: Reference to undeclared namespace prefix: 'rs'.

Rahul
I'm realy sorry. I owerlooked one line in my testing.

EXEC sp_xml_preparedocument @idoc OUTPUT, @doc,'<root xmlns:rs="urn:myRS" xmlns:z="urn:myZ" />'

Last parameter is used namespace definition and must corelate to namespace in xml doc.

Avatar of khinvra

ASKER

Please correct my script and post it here so that I can test it

Rahul
ASKER CERTIFIED SOLUTION
Avatar of patrikt
patrikt
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of khinvra

ASKER

Great !!!