Getting Started with ASP.NET MVC - Part 5: How to do programming with razor syntax
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


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
XQuery 69
TSQL 67
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
August 2009 19
June 2009 19
May 2010 18
January 2009 15
July 2008 15
January 2010 14
October 2008 14
February 2010 12

XQuery Lab 47 – Generating HTML table from XML Data

Aug 19 2009 7:00AM by Jacob Sebastian   

Welcome to XQuery Lab 47. In this lab, we will see how to generate an HTML table from an XML document, using XQuery.

Here is the source data:

<CUST COMPANY="Company1" CONTACT="Jacob" />
<CUST COMPANY="Company1" CONTACT="Michael" />
<CUST COMPANY="Company3" CONTACT="Steve" />

 

Here is the output required

<table>
<tr>
<td>Company</td>
<td>Contact</td>
</tr>
<tr>
<td>Company1</td>
<td>Jacob</td>
</tr>
<tr>
<td>Company1</td>
<td>Michael</td>
</tr>
<tr>
<td>Company3</td>
<td>Steve</td>
</tr>
</table>

 

Here is the TSQL code using XQuery to generate the required output.

DECLARE @x XML
SELECT @x = '
<CUST COMPANY="Company1" CONTACT="Jacob" />
<CUST COMPANY="Company1" CONTACT="Michael" />
<CUST COMPANY="Company3" CONTACT="Steve" />'


SELECT
@x.query('
<table>
<tr>
<td>Company</td>
<td>Contact</td>
</tr>
{
for $r in CUST
return
<tr>
<td>{data($r/@COMPANY)}</td>
<td>{data($r/@CONTACT)}</td>
</tr>
}
</table>
'
)

/*
<table>
<tr>
<td>Company</td>
<td>Contact</td>
</tr>
<tr>
<td>Company1</td>
<td>Jacob</td>
</tr>
<tr>
<td>Company1</td>
<td>Michael</td>
</tr>
<tr>
<td>Company3</td>
<td>Steve</td>
</tr>
</table>
*/

Next Lab: XQuery Lab 48 - Sorting Query files in SQL Server Management Studio (SSMS) Solution/Project

Previous Lab: XQuery Lab 46 – Extracting Zip Code from an Address Value

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

Tags: XQuery-Functions, XQuery-Labs, XML, XQuery, XQuery Functions, SQL Server XQuery, XQuery in TSQL, XQuery Training, XQuery Tutorial,


Jacob Sebastian
1 · 100% · 22473
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising