Mega Code Archive

 
Categories / ASP.Net Tutorial / XML
 

XslCompiledTransform (C#)

using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Xml; using System.Xml.XPath; using System.Xml.Xsl; public partial class _Default : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)     {         Response.ContentType = "text/xml";         string xsltFile = Path.Combine(Request.PhysicalApplicationPath, "Data.xslt");         string xmlFile = Path.Combine(Request.PhysicalApplicationPath, "Data.xml");         XslCompiledTransform xslt = new XslCompiledTransform();         xslt.Load(xsltFile);         XPathDocument doc = new XPathDocument(xmlFile);         xslt.Transform(doc, new XmlTextWriter(Response.Output));     } } File: Data.xml <?xml version='1.0'?> <bookstore xmlns="http://example.books.com"            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">     <book genre="A"            publicationdate="1981"            ISBN="1-11111-11-0">         <title>title 1</title>         <author>             <first-name>A</first-name>             <last-name>B</last-name>         </author>         <price>8</price>     </book>     <book genre="B"            publicationdate="1999"            ISBN="0-222-22222-2">         <title>title 2</title>         <author>             <first-name>C</first-name>             <last-name>D</last-name>         </author>         <price>11.99</price>     </book> </bookstore> File: Data.xsl <?xml version="1.0" encoding="utf-8" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">     <xsl:template match="/">         <xsl:element name="Authors">             <xsl:apply-templates select="//book"/>         </xsl:element>     </xsl:template>     <xsl:template match="book">         <xsl:element name="Author">             <xsl:value-of select="author/first-name"/>             <xsl:text> </xsl:text>             <xsl:value-of select="author/last-name"/>         </xsl:element>     </xsl:template> </xsl:stylesheet>