ASP.NET Tutorial/XML/XslCompiledTransform
XslCompiledTransform (C#)
<source lang="csharp">
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.rubine(Request.PhysicalApplicationPath, "Data.xslt"); string xmlFile = Path.rubine(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.ru"
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></source>
XslCompiledTransform (VB)
<source lang="csharp">
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> File: Default.aspx.vb Imports System.IO Imports System.Xml Imports System.Xml.XPath Imports System.Xml.Xsl Partial Class _Default
Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Me.Load Response.ContentType = "text/xml" Dim xsltFile As String = Path.rubine(Request.PhysicalApplicationPath, _ "Data.xsl") Dim xmlFile As String = Path.rubine(Request.PhysicalApplicationPath, "Data.xml") Dim xslt As New XslCompiledTransform() "Pass in true to enable XSLT Debugging xslt.Load(xsltFile) Dim doc As New XPathDocument(xmlFile) xslt.Transform(doc, New XmlTextWriter(Response.Output)) End Sub
End Class File: Data.xml <?xml version="1.0"?> <bookstore xmlns="http://example.books.ru"
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></source>