Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 

Go Back   XSL - XML - RSS Forums > XML General > SQL and XML

Tags:



Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-17-2008, 04:48 AM
dnandhak
 
Posts: n/a

Default XQuery join nodes



Hi,
Is there a way to join nodes using for loop. for example i have

<name>
<alt>test1</alt>
<alt>test2</alt>
<alt>test3</alt>
</name>

here i want the result as test1;test2;test3

is there a posibility to achieve this.

thanks

----------
dnandhak
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-17-2008, 08:00 PM
Bob
 
Posts: n/a

Default Re: XQuery join nodes

Another trick is to use FOR XML:

SELECT STUFF( ( SELECT ';' + x.y.value( '.','VARCHAR(10)') FROM
@x.nodes('name/alt') x(y) FOR XML PATH('') ), 1, 1, '' )

According to the execution plans for this example, and Martins, it's 98% /
2% to me, proving how expensive FLOWR statements can be.

HTH
wBob

"Martin Honnen" wrote:

> dnandhak wrote:
>
> > Is there a way to join nodes using for loop. for example i have
> >
> > <name>
> > <alt>test1</alt>
> > <alt>test2</alt>
> > <alt>test3</alt>
> > </name>
> >
> > here i want the result as test1;test2;test3

>
> With XQuery 1.0 you can simply do
> string-join(/name/alt, ';')
>
> Unfortunately MS SQL Server does not provide the string-join function so
> it is getting ugly:
>
> DECLARE @x xml;
> SET @x = N'<name>
> <alt>test1</alt>
> <alt>test2</alt>
> <alt>test3</alt>
> </name>';
>
> SELECT @x.query('(for $a in name/alt[position() != last()] return
> concat($a, ";"), data(name/alt[position() = last()]))');
>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Contact Us -|- XSL - XML - RSS Forums -|- Archive -|- Top -|-Rules/Disclaimer-|-Help/Support -|-Advertise
© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
All times are GMT. The time now is 05:49 PM.
Style Developed by Epic Designz