Wednesday, October 13, 2010

How to transform XML file to HTML format using XSLT


Download supported files:

protected override void OnLoad(EventArgs e)
    string html = this.Transform(Server.MapPath("~/Companies.xml"), Server.MapPath("~/CompaniesHtml.xsl"));
public string Transform(string sXmlPath, string sXslPath)
    XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
    XslTransform myXslTrans = new XslTransform();
    StringBuilder output = new StringBuilder();
    using (XmlTextWriter myWriter = new XmlTextWriter(new StringWriter(output)))
        myXslTrans.Transform(myXPathDoc, null, myWriter);
    return output.ToString();
 XML File - Companies.xml
    <name>Mirosoft Ltd</name>
    <address1>Microsoft Campus</address1>
    <address2>Some avenue</address2>
    <postcode>RG6 1WG</postcode>
    <country>United Kingdom</country>
    <address2> Nicholsons Walk</address2>
    <postcode>SL6 1LL</postcode>
    <country>United Kingdom</country>
    <address1>Off Alexander Road</address1>
    <address2>Park Road</address2>
    <city>City of London</city>
    <postcode>TW3 1JT‎</postcode>
    <country>United Kingdom</country>

XSLT file - CompaniesHtml.xsl
<xsl:stylesheet xmlns:xsl="" version="1.0">
  <xsl:output method="html" />
  <xsl:template match="/">
         Clients List - Company Ltd.
          body {font-family:Tahoma,Arial; font-size:9pt;}
        <h1>Our Clients</h1>
        <xsl:for-each select="companies/company">
            <xsl:value-of select="name"/>
            <xsl:value-of select="address1"/>
            <xsl:value-of select="address2"/>
            <xsl:value-of select="city"/>
            <xsl:value-of select="county"/>
            <xsl:value-of select="postcode"/>
            <xsl:value-of select="country"/>
          <hr />

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...