Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reading XML File Created by ADO in SQL Server using OpenXML

Posted on 2004-08-12
8
Medium Priority
?
1,456 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

610 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