Demo:
Download supported files:
Code:
protected override void OnLoad(EventArgs e) { base.OnLoad(e); string html = this.Transform(Server.MapPath("~/Companies.xml"), Server.MapPath("~/CompaniesHtml.xsl")); this.Response.Clear(); this.Response.Write(html); this.Response.End(); } public string Transform(string sXmlPath, string sXslPath) { XPathDocument myXPathDoc = new XPathDocument(sXmlPath); XslTransform myXslTrans = new XslTransform(); myXslTrans.Load(sXslPath); StringBuilder output = new StringBuilder(); using (XmlTextWriter myWriter = new XmlTextWriter(new StringWriter(output))) { myXslTrans.Transform(myXPathDoc, null, myWriter); myWriter.Close(); } return output.ToString(); }XML File - Companies.xml
<companies> <company> <name>Mirosoft Ltd</name> <address1>Microsoft Campus</address1> <address2>Some avenue</address2> <city>Reading</city> <county>Berkshire</county> <postcode>RG6 1WG</postcode> <country>United Kingdom</country> </company> <company> <name>Tesco</name> <address1>65</address1> <address2> Nicholsons Walk</address2> <city>Maidenhead</city> <county>Bukinghamshire</county> <postcode>SL6 1LL</postcode> <country>United Kingdom</country> </company> <company> <name>Tesco</name> <address1>Off Alexander Road</address1> <address2>Park Road</address2> <city>City of London</city> <county>London</county> <postcode>TW3 1JT</postcode> <country>United Kingdom</country> </company> </companies>
XSLT file - CompaniesHtml.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html" /> <xsl:template match="/"> <html> <head> <title> Clients List - Company Ltd. </title> <style> body {font-family:Tahoma,Arial; font-size:9pt;} </style> </head> <body> <h1>Our Clients</h1> <xsl:for-each select="companies/company"> <h2> <xsl:value-of select="name"/> </h2> <p> <xsl:value-of select="address1"/> </p> <p> <xsl:value-of select="address2"/> </p> <p> <xsl:value-of select="city"/> </p> <p> <xsl:value-of select="county"/> </p> <p> <xsl:value-of select="postcode"/> </p> <p> <xsl:value-of select="country"/> </p> <hr /> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>
No comments:
Post a Comment