Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

TSQL Split XML attributes Expand / Collapse
Author
Message
Posted Sunday, November 18, 2012 8:41 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, December 20, 2012 7:03 AM
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
Post #1386137
Posted Sunday, November 18, 2012 10:02 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:01 PM
Points: 3,609, Visits: 5,222
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!
Post #1386145
Posted Monday, November 19, 2012 3:03 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, July 4, 2014 3:55 AM
Points: 2,836, Visits: 5,062
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1386255
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse