Replacing a complex XML node

Reforced

New Member
I need to replace complex nodes within XML. I have no say over the XML schema. I have 2 sources of XML with the same schema. In the below XML, XML-1, I want to replace the Form node where the FormType is Common. The node I replace it with is in XML-2, where FormType is Common and the Field1 data in XML-1 matches Field1 in XML-2.\[code\]<root> <Info>XYZ</Info> <Plan PlanName="ABC" Year="2011"> <Form FormType="Common" Detail=""> <FormData FieldName="Field1">DEF</FormData> <FormData FieldName="Field2">GHI</FormData> <FormData FieldName="Field3">JKL</FormData> <FormData FieldName="Field4">MNO</FormData> </Form> </Plan> <Plan PlanName="BBB" Year="2011"> <Form FormType="Common" Detail=""> <FormData FieldName="Field1">CCC</FormData> <FormData FieldName="Field2">DDD</FormData> <FormData FieldName="Field3">EEE</FormData> <FormData FieldName="Field4">FFF</FormData> </Form> <Form FormType="Specific" Detail=""> <FormData FieldName="Blah1">QQQ</FormData> <FormData FieldName="Blah2">123</FormData> <FormData FieldName="Blah3">ASD</FormData> <FormData FieldName="Blah4">ZZZ</FormData> </Form> </Plan></root>\[/code\]I want to do it with SQL Server, but I am open to any solution. My idea is to load the XML into a table with one row per plan with these fields:\[code\]PlanName varchar(max), Year varchar(5), Field1 varchar(max), FormCommon xml, FormOther xml\[/code\]Then I would reassemble the xml after switching the Common Form node. I can't figure out how to get the information I need.\[code\]declare @handle intdeclare @xml xmlSELECT @xml = ' <...XML...> 'EXEC sp_xml_preparedocument @handle OUTPUT, @xmlselect * FROM OPENXML (@handle, '/root/Plan',2) with ( PlanName varchar(max), Year varchar(5), Form xml, Form2 xml)EXEC sp_xml_removedocument @handle\[/code\]This gives me NULL for PlanName, Year, and Form2. And I get the xml for the first Form node. If I leave out the schema, I get id, nodetype and parentid and other details. I can't figure out how to get all the data I need.Thanks for any help.
 
Back
Top