?
Solved

Reading XML File Created by ADO in SQL Server using OpenXML

Posted on 2004-08-12
8
Medium Priority
?
1,444 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:khinvra
[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
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:patrikt
ID: 11781309
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
0
 

Author Comment

by:khinvra
ID: 11781372
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
0
 
LVL 12

Expert Comment

by:patrikt
ID: 11781428
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
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:khinvra
ID: 11781446
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
0
 
LVL 12

Expert Comment

by:patrikt
ID: 11781464
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.

0
 

Author Comment

by:khinvra
ID: 11781476
Please correct my script and post it here so that I can test it

Rahul
0
 
LVL 12

Accepted Solution

by:
patrikt earned 2000 total points
ID: 11781486
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,'<root xmlns:rs="urn:myRS" xmlns:z="urn:myZ" />'
-- 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
0
 

Author Comment

by:khinvra
ID: 11781494
Great !!!
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

777 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