Solved

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

Posted on 2010-08-24
7
530 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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now