SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


normalizing XML data via transformation


normalizing XML data via transformation

Author
Message
sean hawkes
sean hawkes
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 Visits: 357
I'm a newbie with XML, but i was wondering if somebody could get me started down the path of how to normalize out some XML data. I *think* if I can do this via XSLT, but not sure of what to research. Basically I have nodes that across the document have repeating groups, and I'm curious if I can normalize them into a common node. I'm hoping that I can use this as part of an SSIS package. I would have the first XSD, and the second XSD, what I don't have is the "transalation" peice.

For example:
...
<Appointments>
<Appointment id = "1">
<person>
<Name>John Doe</Name
</person>
<location>
<name>Main</name>
<address>456 Oak St.</address>
<State>NY</State>
</location>
</Appointment>
<Appointment id = "2">
<person>
<Name>Jane Doe</Name
</person>
<location>
<name>Main</name>
<address>456 Oak St.</address>
<State>NY</State>
</location>
</Appointment>
<Appointment id = "3">
<person>
<Name>george Doe</Name
</person>
<location>
<name>Satellite</name>
<address>123 Main St.</address>
<State>NY</State>
</location>
</Appointment>
</Appointments>
...

How do I get that to be something more like
...
<Appointments>
<location>
<name>Main</name>
<address>456 Oak St.</address>
<State>NY</State>
</location>
<location>
<name>Satellite</name>
<address>123 Main St.</address>
<State>NY</State>
</location>
<Appointment id = "3">
<person>
<Name>george Doe</Name
</person>
<location name = "Satellite"/>
</Appointment>
<Appointment id = "2">
<person>
<Name>Jane Doe</Name
</person>
<location name ="Main"/>
</Appointment>
<Appointment id = "1">
<person>
<Name>John Doe</Name
</person>
<location name ="Main"/>
</Appointment>
</Appointments>
...

Or something like that. I'm not that concerned with element v attribute, i'm more interested in how would I do the "normalization" peice. Right now being a DBA, i'm more inclined to load it to a staging database and do the "translation" via T-SQL, but I'm going to attempt to broaden my mind (scary).

Thanks in advance,
Sean
Arthur Olcot
Arthur Olcot
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3184 Visits: 1779
Personally I would use XQuery to do the transformation. I save xslt for the hardest of transformations, but to be honest, I have been able to do most things in XQuery. If the data is already stored in your sql tables, this makes it a bit easier as you can use tsql and not have to worry about creating a clr or another app/ssis to read the data out, do the transformation and save it back.

for example, I quickly created the following based on your example you documented to do the transformation using xquery:


DECLARE @xml XML
SET @xml = '<Appointments>
<Appointment id = "1">
<person>
<Name>John Doe</Name>
</person>
<location>
<name>Main</name>
<address>456 Oak St.</address>
<State>NY</State>
</location>
</Appointment>
<Appointment id = "2">
<person>
<Name>Jane Doe</Name>
</person>
<location>
<name>Main</name>
<address>456 Oak St.</address>
<State>NY</State>
</location>
</Appointment>
<Appointment id = "3">
<person>
<Name>george Doe</Name>
</person>
<location>
<name>Satellite</name>
<address>123 Main St.</address>
<State>NY</State>
</location>
</Appointment>
</Appointments>'

SELECT @xml.query('for $x in /Appointments return
<Appointments>
{
for $y in distinct-values($x/Appointment/location/name/text())
return
($x/Appointment/location[name = $y])[1]

}
{
for $y in $x/Appointment
return
<Appointment id="{$y/@id}">
{$y/person}
<location name="{$y/location/name/text()}"/>
</Appointment>
}
</Appointments>
')



You can see the main guts of the code is quite small and remodels the xml as you require.

http://msdn.microsoft.com/en-us/library/ms189075.aspx
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search