Solved

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

Posted on 2010-08-24
7
538 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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