Solved

Reading XML File Created by ADO in SQL Server using OpenXML

Posted on 2004-08-12
8
1,432 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

688 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