Solved

How to concatenate column value (xml) into one big xml

Posted on 2010-08-24
7
537 Views
Last Modified: 2012-08-13
Hello Experts -

I need your help with this one.

I have a sql table column (varchar field) containing xml data in the following format

<root>
<element value='A1'>A1<element>
<element value='A2'>A2<element>
<element value='A3'>A3<element>
<element value='A4'>A4<element>
</root>

I am trying to concatenate this column value for table rows matching some criteria into one big xml.
While creating this new xml i need to add row identifier element and update the xml values.
The final xml output should have the following layout

<root>
<element value='1'>RowA<element>
<element value='2'>A1<element>
<element value='3'>A2<element>
<element value='4'>A3<element>
<element value='5'>A4<element>
<element value='6'>RowB<element>
<element value='7'>B1<element>
<element value='8'>B2<element>
<element value='9'>B3<element>
<element value='10'>B4<element>
</root>

I prefer to do it on sql side (in stored proc). If that is not feasible, I can do it C# too.

What is the best approach for this?

Thanks
0
Comment
Question by:leo2007
  • 4
  • 3
7 Comments
 
LVL 5

Expert Comment

by:tvPrasad
ID: 33514857
You can get this update done at SQL Stored Proc using XQuery.
0
 
LVL 5

Expert Comment

by:tvPrasad
ID: 33514958
Above supplied sample xml is incomplete.

Here is the complete xml ...

<root>
<element value='A1'>A1</element>
<element value='A2'>A2</element>
<element value='A3'>A3</element>
<element value='A4'>A4</element>
</root>


<root>
<element value='1'>RowA</element>
<element value='2'>A1</element>
<element value='3'>A2</element>
<element value='4'>A3</element>
<element value='5'>A4</element>
<element value='6'>RowB</element>
<element value='7'>B1</element>
<element value='8'>B2</element>
<element value='9'>B3</element>
<element value='10'>B4</element>
</root>
0
 

Author Comment

by:leo2007
ID: 33515047
Could you please an example - for column (xml) concatenation with Xquery?
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:leo2007
ID: 33515436
oops! Typo.I meant to say - could you please provide an example - to concatenate column values from table rows?
0
 
LVL 5

Accepted Solution

by:
tvPrasad earned 500 total points
ID: 33515468
Here are few queries... you have to tailor for you use ...

DECLARE @x xml
SET @x='<root><element value="A1">A1</element><element value="A2">A2</element><element value="A3">A3</element><element value="A4">A4</element></root>'

SELECT @x.query('<root> { /root/element }  </root>')
SELECT @x.query('<root> {
if(count(/root/element)<0)
then (string(count(/root/element/*)))
else
(string(count(/root/element)))
 }  </root>')
select @x.query('
<root> { for $i in /root/element where count($i)>0      
    return  <element value="{ data(/root/element) }"> { string($i) } </element>
} </root>')
select @x.query('
<root> { for $i in /root/element
    return  <element value="{ string(count(/root/element)) }"> { string($i) } </element>
} </root>')
select @x.query('
<root> { for $i in /root/element
    return  <element value="{ string(count(/root/element/* [position()] )) }"> { string($i) } </element>
} </root>')
0
 

Author Comment

by:leo2007
ID: 33538405
TvPrasad - Thanks for the queries. How do i loop through all the elements of the xml and do the following
a) assign the loop counter as the element value
b) change the element text (append text to exisiting text)


<root>
<element value='1'>RowA</element>
<element value='2'>xyz A1</element>
<element value='3'>xyz A2</element>
<element value='4'>xyz A3</element>
<element value='5'>A4</element>
</root>




0
 

Author Closing Comment

by:leo2007
ID: 33642097
The  solution provided was incomplete and follow up question went unanswered.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question