SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


xml to html


xml to html

Author
Message
EamonSQL
EamonSQL
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1135 Visits: 196
Hi,

I'm working on a script at the moment that pulls groups of data out with each group represented as xml data.
I'd like to transform the xml using a stylesheet into html then output the html as a table using a sql script.

Is there any good tutorials out there that someone can recommend.

Any help welcome on this.

Thanks,
Eamon
Arthur Olcot
Arthur Olcot
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4642 Visits: 1779
Hi,
If you want to use a stylesheet then your options are going to be limited if you want to keep it all inside sql. SSIS supports xsl transformations via the XML task, although I have rarely used this myself so can't really advise on that http://technet.microsoft.com/en-us/library/ms141055.aspx.

SQL CLR is another option if you are able/want to head in that direction.

How complicated is the HTML that you are generating via the stylesheet? As if it is fairly simple, then in theory you could build the html on the fly using t-sql without the need to use a stylesheet.

If you could provide some examples of what the resultant html needs to look like and describe your source table structures then that might help. how is the resultant html going to be consumed?

Personally unless this is only going to be used on an ad-hoc type basic I'd be inclined to move the html generation into an application tier rather than getting sql to generate the HTML itself on a regular basis.
EamonSQL
EamonSQL
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1135 Visits: 196
Thanks for this.

I kind of expected a reply like this as I've looked around quite a bit and always ended up with CLR or SSIS offering a solution.
Building the html on the fly I think is my best option here as the output is quite basic with only 4 columns of data.
I have already played around with and works just fine.

I very appreciate you advice and quick reply.

Kind regards,
Eamon
Alan Burstein
Alan Burstein
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32524 Visits: 8578
I know this is a few days old but I thought it would be good to expand on this a little:

The W3C defines two technologies for "Transforming XML": SQLXML/XQuery and XSLT
A limited version of XQuery is available in XML but, as arthurolcot mentioned, it would likely do the trick (especially if you are dealing with 4 simple columns). XML Transforms (XSLT) are the more elegant way to go IMHO but the only way to run XSLT is via SSIS or SQLCLR.

On that note, if you go the SQLCLR route you can use mdq.xmltranform (a CLR written by Microsoft) to transform XML to HTML. You can also build an XSLT CLR from scratch but mdq.xmltransform has already been compiled and takes one minute to setup (no need to create a new DLL). I wrote a couple articles about how to do transforms in SQL Server. mdq.xmltransform will be slower than using XQuery but it will work and the code will be 99.9% portable. Doing the transforms using SSIS will perform butter (but will also require you to be comfortable with SSIS which not everone is).

A few good sites about transforming XML using XSLT:
http://w3schools.com/xpath/default.asp
http://w3schools.com/xsl/default.asp
http://www.xml.com/pub/a/2000/08/holman/index.html?page=5#sample1



Edit: Added note about not needing to compile a dll for the CLR

-- Alan Burstein


Helpful links:

Best practices for getting help on SQLServerCentral -- Jeff Moden
How to Post Performance Problems -- Gail Shaw

Nasty fast set-based string manipulation functions:
For splitting strings try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL Server 2012+)
To split strings based on patterns try PatternSplitCM
Need to clean or transform a string? try NGrams, PatExclude8K, PatReplace8K, DigitsOnlyEE, or Translate8K

I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. -- Itzik Ben-Gan 2001

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search