Solved

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

Posted on 2010-08-24
7
534 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem!!! 8 33
Following an example - Why do I need to click Save button twice? 3 25
Text file into sql server 5 23
VB.NET 2008 - SQL Timeout 9 24
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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