Solved

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

Posted on 2010-08-24
7
540 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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