x

Converting XML Attribute to Element

In my incoming third-party XML, how do I change the Attribute to be an element?

That way, instead of reading this:

 <ROW TBL="ACCTDIRECTORY">
     <CONTACTTYPE V="SERVICE_ADDRESS"/>
     <ADDRESS1 V="4320 Acme Rd"/>
     <CITY V="FunTown"/>
     <STATE V="FL"/>
     <ZIP V="33333"/>
 </ROW>

it will be readable as this:

 <ROW TBL="ACCTDIRECTORY">
     <CONTACTTYPE>SERVICE_ADDRESS<CONTACTTYPE/>
     <ADDRESS1>4320 Acme Rd<ADDRESS1/>
     <CITY>FunTown<CITY/>
     <STATE>FL<STATE/>
     <ZIP>33333<ZIP/>
 </ROW>

I saw snippets about how to use XSLT to do this kind of work. Is it possible to do this using SSIS?

more ▼

asked Apr 13, 2011 at 02:39 PM in Default

avatar image

Raj More
1.8k 82 87 90

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

Something like the following would work in SQL Server, but I don't think it is the right tool for the job. An XSLT would probably be much better but, since this is a SQL Server Q&A board, let's go with that:

 DECLARE @xml XML = '
     <ROWS>
         <ROW TBL="ACCTDIRECTORY">
             <CONTACTTYPE V="1SERVICE_ADDRESS"/>
             <ADDRESS1 V="4320 Acme Rd"/>
             <CITY V="FunTown"/>
             <STATE V="FL"/>
             <ZIP V="33333"/>
         </ROW>
         <ROW TBL="ACCTDIRECTORY">
             <CONTACTTYPE V="2SERVICE_ADDRESS"/>
             <ADDRESS1 V="4322 Acme Rd"/>
             <CITY V="FunGown"/>
             <STATE V="FI"/>
             <ZIP V="33433"/>
         </ROW>
     </ROWS>
 '

 SELECT 
     CONTACTTYPE = ROW.x.value('./CONTACTTYPE[1]/@V', 'nvarchar(max)'),
     ADDRESS1 = ROW.x.value('./ADDRESS1[1]/@V', 'nvarchar(max)'),
     CITY = ROW.x.value('./CITY[1]/@V', 'nvarchar(max)'),
     STATE = ROW.x.value('./STATE[1]/@V', 'nvarchar(max)'),
     ZIP = ROW.x.value('./ZIP[1]/@V', 'nvarchar(max)')
 FROM @xml.nodes('//ROW') ROW(x)
 FOR XML PATH('ROW'), ELEMENTS, ROOT('ROWS')

As for SSIS, you can do pretty much anything with a .NET Script Task...

more ▼

answered Apr 13, 2011 at 06:58 PM

avatar image

jimbobmcgee
399 5 8 12

@jimbobmcgee I am doing SSIS, so I could use XSLT using an XML task

Apr 14, 2011 at 03:53 AM Raj More

XSLT is not my forte, so I'll probably bow out here. If I do come up with anything, I'll let you know.

Apr 18, 2011 at 03:46 AM jimbobmcgee
(comments are locked)
10|1200 characters needed characters left

I used XSLT and solved this problem

more ▼

answered Sep 01, 2011 at 02:05 PM

avatar image

Raj More
1.8k 82 87 90

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1152
x748
x4
x4
x1

asked: Apr 13, 2011 at 02:39 PM

Seen: 1537 times

Last Updated: Apr 13, 2011 at 02:51 PM

Copyright 2016 Redgate Software. Privacy Policy