Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Browse by Tags · View All
XML 112
TSQL 69
XQuery 69
XQuery Functions 67
XQuery Training 65
XQuery in TSQL 64
XQuery Tutorial 63
SQL Server XQuery 63
XQuery-Labs 57
BRH 38

Archive · View All
September 2008 32
August 2008 30
July 2008 21
August 2009 19
June 2009 19
May 2010 18
January 2009 15
January 2010 14
October 2008 14
June 2008 13

XQuery Lab 65 – Restructuring an XML document using FLWOR

Dec 9 2011 1:33PM by Jacob Sebastian   

I found this question in this SSC forum thread requesting help to shape the XML document. Here is the input XML document.

<Root>
	<Student>Jhon </Student>
	<Student> Luka </Student>
	<Post>1</Post>
	<Post>2</Post>
</Root>

The expected output is as follows.

<Root>
  <Students>
    <Student>Jhon </Student>
    <Student> Luka </Student>
  </Students>
  <Posts>
    <Post>1</Post>
    <Post>2</Post>
  </Posts>
</Root>

This type of formatting can be achieved through a simple FLWOR operation.

DECLARE @x XML = '
<Root>
	<Student>Jhon </Student>
	<Student> Luka </Student>
	<Post>1</Post>
	<Post>2</Post>
</Root>'

SELECT @x.query ('
	for $i in (Root)
	let $s := $i/Student
	let $p := $i/Post
	return
		<Root>
			<Students> {$s} </Students>
			<Posts>{$p}</Posts>
		</Root>
')

/*
Produces: 
<Root>
  <Students>
    <Student>Jhon </Student>
    <Student> Luka </Student>
  </Students>
  <Posts>
    <Post>1</Post>
    <Post>2</Post>
  </Posts>
</Root>
*/

View All Labs: XQuery Labs - A Collection of XQuery Sample Scripts and Tutorials

Tags: XQuery, XQuery Tutorials, XQuery Lab, XQuery Functions, XQuery in TSQL, XQuery Training, XQuery Tutorial,


Jacob Sebastian
1 · 100% · 32235
1 Readers said this post helped them to refresh memory
bmorstad said this post helped them to refresh memory on 3/2/2012 6:57:00 AM
Profile
8
 
0
Lifesaver
 
1
Refreshed
 
 
0
Incorrect



Submit

2  Comments  

  • Nice. I always learn something new from your XML related articles/post.

    Thanks for sharing.

    commented on Dec 12 2011 1:26AM
    Hardik Doshi
    20 · 9% · 2853
  • Glad to hear that you found it helpful.

    commented on Dec 12 2011 1:43AM
    Jacob Sebastian
    1 · 100% · 32235

Your Comment


Sign Up or Login to post a comment.

"XQuery Lab 65 – Restructuring an XML document using FLWOR" rated 5 out of 5 by 8 readers
XQuery Lab 65 – Restructuring an XML document using FLWOR , 5.0 out of 5 based on 8 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]