﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / TSQL Split XML attributes / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 22 May 2013 09:45:08 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: TSQL Split XML attributes</title><link>http://www.sqlservercentral.com/Forums/Topic1386137-392-1.aspx</link><description>[quote][b]pricejt (11/18/2012)[/b][hr]I have an xml string and need to manipulate it within sql....[/quote]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)</description><pubDate>Mon, 19 Nov 2012 03:03:39 GMT</pubDate><dc:creator>Eugene Elutin</dc:creator></item><item><title>RE: TSQL Split XML attributes</title><link>http://www.sqlservercentral.com/Forums/Topic1386137-392-1.aspx</link><description>To start with, the XML you provided won't parse:[code="sql"]DECLARE @XML XML = '    &amp;lt;RuleData ReturnVariable=""&amp;gt;      &amp;lt;MateProperties&amp;gt;        &amp;lt;Property Key="value" DisplayName="name value" /&amp;gt;        &amp;lt;Property Key="value2" DisplayName="name value2" /&amp;gt;      &amp;lt;/MateProperties&amp;gt;      &amp;lt;ReferenceNames&amp;gt;        &amp;lt;Item Name="0" Value="=CouplerType + "-1"" /&amp;gt;        &amp;lt;Item Name="1" Value="Driveshaft-1" /&amp;gt;      &amp;lt;/ReferenceNames&amp;gt;      &amp;lt;ReferenceFeatures&amp;gt;        &amp;lt;Item Name="=CouplerType + "-1"" Value="CSYS_BOREB" /&amp;gt;        &amp;lt;Item Name="Driveshaft-1" Value="CSYS_CouplerINSIDE" /&amp;gt;      &amp;lt;/ReferenceFeatures&amp;gt;    &amp;lt;/RuleData&amp;gt;'SELECT @XML[/code]Gets this error message:[code="plain"]Msg 9410, Level 16, State 1, Line 1XML parsing: line 8, character 43, whitespace expected[/code]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.</description><pubDate>Sun, 18 Nov 2012 22:02:36 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>TSQL Split XML attributes</title><link>http://www.sqlservercentral.com/Forums/Topic1386137-392-1.aspx</link><description>I have an xml string and need to manipulate it within sql.[code="xml"]&amp;lt;RuleData ReturnVariable=""&amp;gt;  &amp;lt;MateProperties&amp;gt;    &amp;lt;Property Key="value" DisplayName="name value" /&amp;gt;    &amp;lt;Property Key="value2" DisplayName="name value2" /&amp;gt;  &amp;lt;/MateProperties&amp;gt;  &amp;lt;ReferenceNames&amp;gt;    &amp;lt;Item Name="0" Value="=CouplerType + &amp;quot;-1&amp;quot;" /&amp;gt;    &amp;lt;Item Name="1" Value="Driveshaft-1" /&amp;gt;  &amp;lt;/ReferenceNames&amp;gt;  &amp;lt;ReferenceFeatures&amp;gt;    &amp;lt;Item Name="=CouplerType + &amp;quot;-1&amp;quot;" Value="CSYS_BOREB" /&amp;gt;    &amp;lt;Item Name="Driveshaft-1" Value="CSYS_CouplerINSIDE" /&amp;gt;  &amp;lt;/ReferenceFeatures&amp;gt;&amp;lt;/RuleData&amp;gt;[/code]These two lines I need to convert them.[code="xml"]&amp;lt;Item Name="=CouplerType + &amp;quot;-1&amp;quot;" Value="CSYS_BOREB" /&amp;gt;&amp;lt;Item Name="Driveshaft-1" Value="CSYS_CouplerINSIDE" /&amp;gt;[/code]To This new format.[code="xml"]&amp;lt;Value&amp;gt; &amp;lt;Column Value="=CouplerType + &amp;quot;-1&amp;quot;" /&amp;gt; &amp;lt;Column Value="CSYS_BOREB" /&amp;gt; &amp;lt;/Value&amp;gt; &amp;lt;Value&amp;gt; &amp;lt;Column Value="Driveshaft-1" /&amp;gt; &amp;lt;Column Value="CSYS_CouplerINSIDE" /&amp;gt; &amp;lt;/Value&amp;gt;[/code]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</description><pubDate>Sun, 18 Nov 2012 20:41:13 GMT</pubDate><dc:creator>pricejt</dc:creator></item></channel></rss>