xml to html

  • 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

  • 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.

  • 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

  • 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

    "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

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply