Solved

Reading XML File Created by ADO in SQL Server using OpenXML

Posted on 2004-08-12
8
1,404 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
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:patrikt
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:khinvra
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 12

Expert Comment

by:patrikt
Comment Utility
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
Comment Utility
Please correct my script and post it here so that I can test it

Rahul
0
 
LVL 12

Accepted Solution

by:
patrikt earned 500 total points
Comment Utility
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
Comment Utility
Great !!!
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now