Solved

XML Get values in ColumnA

Posted on 2011-02-17
2
928 Views
Last Modified: 2012-05-11
I am trying to get the TSQL code in SQL Server to parse the XML to show records in ColumnA
declare @XmlDoc xml, 
@iDoc integer 
select @XmlDoc = '<VendorCodesRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" EchoToken="String" TimeStamp="2011-02-17T16:29:05" Version="2003A.TsabreXML1.0.1" SequenceNmbr="1" PrimaryLangID="en-us" AltLangID="en-us">
  <Success xmlns="http://webservices/XML/2003/07" />
  <VendorInfo xmlns="http://webservices/XML/2003/07">
    <Row RPH="1">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB />
      <ColumnC>
        <Text>LetterA</Text>
      </ColumnC>
    </Row>
    <Row RPH="2">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB>
        <Text>AB</Text>
      </ColumnB>
      <ColumnC>
        <Text>LetterAB</Text>
      </ColumnC>
    </Row>
    <Row RPH="3">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB>
        <Text>AC</Text>
      </ColumnB>
      <ColumnC>
        <Text>LetterAC</Text>
      </ColumnC>
    </Row>
  </VendorInfo>
  <TPA_Extensions xmlns="http://webservices/XML/2003/07">
    <HostCommand>DU*/CLS</HostCommand>
  </TPA_Extensions>
</VendorCodesRS>' 

exec sp_xml_preparedocument @iDoc output, @XmlDoc
select * from openxml
(@iDoc, 'VendorInfo',2) 
WITH 
(
ColumnA varchar(max) 'ColumnA/Text'
)

EXEC sp_xml_removedocument @idoc

Open in new window

0
Comment
Question by:garbertravel
2 Comments
 
LVL 37

Accepted Solution

by:
ValentinoV earned 500 total points
ID: 34924044
I found a way to make it work if I remove the namespace definition from the VendorInfo node (see snippet).  But unfortunately I haven't succeeded in getting it to work without any modifications to the XML.

Hopefully this will get you going though?
declare @XmlDoc xml = '<VendorCodesRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" EchoToken="String" TimeStamp="2011-02-17T16:29:05" Version="2003A.TsabreXML1.0.1" SequenceNmbr="1" PrimaryLangID="en-us" AltLangID="en-us">
  <Success xmlns="http://webservices/XML/2003/07" />
  <VendorInfo>
    <Row RPH="1">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB />
      <ColumnC>
        <Text>LetterA</Text>
      </ColumnC>
    </Row>
    <Row RPH="2">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB>
        <Text>AB</Text>
      </ColumnB>
      <ColumnC>
        <Text>LetterAB</Text>
      </ColumnC>
    </Row>
    <Row RPH="3">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB>
        <Text>AC</Text>
      </ColumnB>
      <ColumnC>
        <Text>LetterAC</Text>
      </ColumnC>
    </Row>
  </VendorInfo>
  <TPA_Extensions xmlns="http://webservices/XML/2003/07">
    <HostCommand>DU*/CLS</HostCommand>
  </TPA_Extensions>
</VendorCodesRS>';

select Col.value(N'./Text[1]', N'nvarchar(100)') as ColumnA
from @XmlDoc.nodes(N'//VendorInfo/Row/ColumnA') Tab(Col);

Open in new window

0
 

Author Closing Comment

by:garbertravel
ID: 34929971
The data resides in a SQL field and I am just doing a find replace before loading the data. Thanks!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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

919 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

22 Experts available now in Live!

Get 1:1 Help Now