Use XslTransform to trasnform XML data to HTML file (VB.net) : XslTransform « XML « ASP.NET Tutorial






<%@ Page Language="VB" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>
<%@ Import Namespace="System.Xml.Xsl" %>

<script runat="server">
   sub Page_Load(Sender as Object, e as EventArgs)
      Dim objReader as New XmlTextReader(Server.MapPath("Data.xml"))
      
      Dim objDoc as XPathDocument = new XPathDocument(objReader)
      Dim objNav as XPathNavigator = objDoc.CreateNavigator()
      
      Dim objXSLT As XslTransform = New XslTransform()
      dim objWriter as XmlTextWriter = new XmlTextWriter(Server.MapPath("output.html"), nothing)

      try
         objXSLT.Load(Server.MapPath("Data.xslt"))
         objXSLT.Transform(objDoc, nothing, objWriter)

         Response.Write("File written successfully")
      catch ex As Exception        
         Response.Write(ex.Message)
      finally
         objReader.Close
         objWriter.Close
      end try
   end sub   
</script>

<html><body>
</body></html>


File: Data.xml


<?xml version="1.0"?>
<bookstore>
  <book genre="asdf">
    <title>asdf</title>
    <author>
      <first-name>asdf</first-name>
      <last-name>asdf</last-name>
    </author>
    <price>asdf</price>
  </book>
  <book genre="asdf">
    <title>asdf</title>
    <author>
      <first-name>asdf</first-name>
      <last-name>asdf</last-name>
    </author>
    <price>asdf</price>
  </book>
  <book genre="asdf">
    <title>asdf</title>
    <author>
      <first-name>asdf</first-name>
      <last-name>asdf</last-name>
    </author>
    <price>asdf</price>
  </book>
</bookstore>

File: Data.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:op="x-schema:books.xdr"
   version="1.0" >
   <xsl:template match="op:bookstore">
      <HTML><BODY>
      <TABLE width="450">
      <TR>
         <TD><b>Title</b></TD>
         <TD><b>Price</b></TD>
      </TR>
      <xsl:apply-templates select="op:book"/>
      </TABLE>
      </BODY></HTML>
   </xsl:template>
   <xsl:template match="op:book">
      <TR>
         <TD><xsl:value-of select="op:title"/></TD>
         <TD><xsl:value-of select="op:price"/></TD>
      </TR>
   </xsl:template>
</xsl:stylesheet>








25.19.XslTransform
25.19.1.Use XslTransform to trasnform XML data to HTML file (VB.net)
25.19.2.Read the transformed result of XslTransform and output the result (VB.net)
25.19.3.Display data from database in asp:Xml with xml transformation
25.19.4.Transform XML document to HTML