Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XML Get values in ColumnA

Posted on 2011-02-17
2
Medium Priority
?
959 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
[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
2 Comments
 
LVL 37

Accepted Solution

by:
ValentinoV earned 2000 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

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