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

Raj More gravatar image

Raj More
1.7k 80 82 84

(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

jimbobmcgee gravatar image

jimbobmcgee
359 5 6 7

@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

Raj More gravatar image

Raj More
1.7k 80 82 84

(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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

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:

x937
x586
x4
x2
x1

asked: Apr 13, 2011 at 02:39 PM

Seen: 1246 times

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