Reading and Transforming XML Documents using Visual Basic 2005 - Transforming an XML document to HTML using XSLT with VB 2005
(Page 5 of 6 )
XSL stands for eXtensible Stylesheet Language. It is a language used to design and apply style sheets especially for XML documents. Originally the research was started to provide style sheet technology to XML using XSL, but we finally ended up with three more divisions of XSL. So XSL now consists of three parts, namely XSLT, XPath and XSL-FO.
XSLT is a language for transforming XML documents (even today, several programmers say XSL when they really mean XSLT). XPath is a language to filter, search or sort information available in XML documents. XSL-FO is a language for formatting XML documents. In this article we mainly focus on XSLT, which stands for XSL Transformations.
XSLT can also be used to transform an XML document to another XML document (it need not be used only for HTML documents). Another beauty of XSLT is that it internally works using XPath language. We can even conclude that “The more we learn about XPath, the better XSLT we can design.” Although the sections from this point forward mainly focus on XSL, I suggest you go through the fundamentals of the XPath language for better understanding (but it's not necessary for this article).
Before transforming an XML document, we need to define the XSLT which emits HTML based on the structure of our existing XML document. The following is the XSLT needed to emit the TABLE structure of HTML:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table width="50%" cellspacing="0" cellpadding="0" style="font-
family:verdana;font-size:X-Small" border="1">
<tr bgcolor="#336699">
<th align="left">
<font color="White">ID</font>
</th>
<th align="left">
<font color="White">Name</font>
</th>
<th align="right">
<font color="White">Salary</font>
</th>
<th align="right">
<font color="White">Deptno</font>
</th>
</tr>
<tr>
<td align="left">
<xsl:value-
of select="Employees/Employee/Empno"/>
</td>
<td align="left">
<xsl:value-
of select="Employees/Employee/Ename"/>
</td>
<td align="right">
<xsl:value-
of select="Employees/Employee/Sal"/>
</td>
<td align="right">
<xsl:value-
of select="Employees/Employee/Deptno"/>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The following code transforms the XML document to the above XSLT:
Private Sub btnSampleXSLT_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnSampleXSLT.Click
Dim docXML As New XmlDocument
docXML.Load(Application.ExecutablePath &
"......Employee.xml")
Dim xslt As New Xsl.XslCompiledTransform
xslt.Load(Application.ExecutablePath &
"......Employee.xslt")
Dim sb As New System.Text.StringBuilder
Dim s As New IO.StringWriter(sb)
xslt.Transform(docXML, New XmlTextWriter(s))
s.Close()
Me.TextBox1.Text = sb.ToString
End Sub
Next: Transformation using XSLT for multiple XML nodes of the same criteria >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee