TSQL Split XML attributes

  • 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

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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[/url]

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply