Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TSQL Split XML attributes


TSQL Split XML attributes

Author
Message
pricejt
pricejt
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 88
I have an xml string and need to manipulate it within sql.


<RuleData ReturnVariable="">
<MateProperties>
<Property Key="value" DisplayName="name value" />
<Property Key="value2" DisplayName="name value2" />
</MateProperties>
<ReferenceNames>
<Item Name="0" Value="=CouplerType + "-1"" />
<Item Name="1" Value="Driveshaft-1" />
</ReferenceNames>
<ReferenceFeatures>
<Item Name="=CouplerType + "-1"" Value="CSYS_BOREB" />
<Item Name="Driveshaft-1" Value="CSYS_CouplerINSIDE" />
</ReferenceFeatures>
</RuleData>



These two lines I need to convert them.

<Item Name="=CouplerType + "-1"" Value="CSYS_BOREB" />
<Item Name="Driveshaft-1" Value="CSYS_CouplerINSIDE" />



To This new format.


<Value>
<Column Value="=CouplerType + "-1"" />
<Column Value="CSYS_BOREB" />
</Value>
<Value>
<Column Value="Driveshaft-1" />
<Column Value="CSYS_CouplerINSIDE" />
</Value>



Essentially each line has to be split in two and re-named. I Can take care of the re-naming i just am having trouble splitting the columns. I have to do exactly the same thing in the ReferenceNames Section as well. That one is easier as Its always "0" and "1" so i can simply convert the xml to string and find replace with that. The ReferenceFeatures section though is more complicated because anything can be in the name section.

Any help would be appreciated.

Thanks
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
To start with, the XML you provided won't parse:


DECLARE @XML XML = '
<RuleData ReturnVariable="">
<MateProperties>
<Property Key="value" DisplayName="name value" />
<Property Key="value2" DisplayName="name value2" />
</MateProperties>
<ReferenceNames>
<Item Name="0" Value="=CouplerType + "-1"" />
<Item Name="1" Value="Driveshaft-1" />
</ReferenceNames>
<ReferenceFeatures>
<Item Name="=CouplerType + "-1"" Value="CSYS_BOREB" />
<Item Name="Driveshaft-1" Value="CSYS_CouplerINSIDE" />
</ReferenceFeatures>
</RuleData>'

SELECT @XML




Gets this error message:


Msg 9410, Level 16, State 1, Line 1
XML parsing: line 8, character 43, whitespace expected



In order to use the modify method in SQL, the XML must be parseable. Otherwise you'll need to resort to doing the replace as a string operation.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
pricejt (11/18/2012)
I have an xml string and need to manipulate it within sql.
...


T-SQL is not the language for XML manipulation. It can parse it and it can create it, but not transform it.
This task is for XSLT.
If you still want to do so in T-SQL you have two choices:
1. Parse it into table then create a new one (stupid option)
or
2. Create CLR function which will consume your XML, relevant XSLT and apply it (preferred option)

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
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