Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

XQuery join nodes

 
   Database Help (Home) -> XML RSS
Next:  Why is my computer saying im in Germany? im in th..  
Author Message
dnandhak

External


Since: Nov 16, 2008
Posts: 1



(Msg. 1) Posted: Sun Nov 16, 2008 8:38 pm
Post subject: XQuery join nodes
Archived from groups: microsoft>public>sqlserver>xml (more info?)

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

 >> Stay informed about: XQuery join nodes 
Back to top
Login to vote
Martin Honnen

External


Since: Apr 20, 2007
Posts: 48



(Msg. 2) Posted: Mon Nov 17, 2008 7:25 am
Post subject: Re: XQuery join nodes [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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/

 >> Stay informed about: XQuery join nodes 
Back to top
Login to vote
Bob

External


Since: Feb 08, 2005
Posts: 182



(Msg. 3) Posted: Mon Nov 17, 2008 11:38 am
Post subject: Re: XQuery join nodes [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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/
>
 >> Stay informed about: XQuery join nodes 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
How to add nodes into xml? - I want to add some nodes into xml variable: For example: The original xml: <PO> <PONO>123</PONO> <PODATE>07252007</PODATE> <POCNAME>ABC Company</PONAME> <POITEMS> ... ... ...

Identifying the nodes using OPENXML - I'm using SQL Server 2000. I must import a document structured in XML. This document has a root, some invoice header nodes and inside a single invoice header some invoice details are present. The invoices data are written as attributes. When I insert th...

finding nodes in xml files - I have xml feed files that we investigate for bugs. The size of these files is quite big (ranging from 500 KB to 1.4 GB). My aim is to search these files for some specific string value in some element text or attribute text and display all resulting..

How to retreive parent nodes and values from a child node? - Hello, I have an XML as below. declare @doc2 xml SET @doc2 = '<?xml version="1.0" ?> <p1:Domains_and_Emails xmlns:p1="LDAP"> <Organization>ABCD</Organization> <Radius Suspended="false" ID=&qu...

modify() Top-level attribute nodes are not supported Error - I'm trying to return the following: <person> <personal_info> ... </personal_info> <prof_quals> ... </prof_quals> </person> The following results in an error: XQuery [#xPerson.xPerson.modify()]: Top-level att...
   Database Help (Home) -> XML All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]