Facebook

Generating HTML from XML, Database, Dataset

HTML from XML file source is generated, but you can use dataset to generate HTML from database by generating XML using dataset. In this example i have used XSLT file to transform XML to HTML file.

Download source code

Code:

string source = Server.MapPath("~") + "//XMLFile.xml";
        string style = Server.MapPath("~") + "//XSLTFile.xslt";
        string destination = Server.MapPath("~") + "//a.html";
        XslTransform transfor=new XslTransform();
        transfor.Load(style);
        transfor.Transform(source,destination);

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>XSLT Sample</title>
            </head>
            <xsl:apply-templates/>
        </html>
    </xsl:template>

    <xsl:template match="inventory">
        <body bgcolor="#ffffff">
            <h1>Heading 1</h1>
            <h2>Heading 2</h2>
            <xsl:apply-templates/>
        </body>
    </xsl:template>

    <xsl:template match="date">
        <p>current as
        <xsl:value-of select="@month"/>/<xsl:value-of select="@day"/>/<xsl:value-of select="@year"/>
        </p>
    </xsl:template>

    <xsl:template match="items">
        <p>currently available items</p>
        <table border="1">
            <tr>
                <th>Product code</th>
                <th>Description</th>
                <th>Unit Price</th>
                <th>Quantity</th>
            </tr>
            <xsl:apply-templates/>
        </table>
    </xsl:template>

    <xsl:template match="item">
        <tr>
            <td>
                <xsl:value-of select="@productcode"/>
            </td>
            <td>
                <xsl:value-of select="@description"/>
            </td>
            <td>
                <xsl:value-of select="@unitcost"/>
            </td>
            <td>
                <xsl:value-of select="@quantity"/>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

Download source code

Leave a Reply

Your email address will not be published. Required fields are marked *