Introduction
You have an XML document and you need to convert it into a more readable file format. For example, you have personnel data that is stored as an XML document and you need to display it on a Web page or in a text file.
Solution
The solution for this is to use an XSLT stylesheet to transform the XML into another format using the XslTransform
class. In the example code, we are transforming some personnel data from a fictitious business stored in Personnel.xml. First, we load the stylesheet for generating HTML output. Then we perform the transformation to HTML via XSLT using the PersonnelHTML.xsl stylesheet. After that, we transform the data to comma-delimited format using the PersonnelCSV.xsl stylesheet:
public static void TransformXML( )
{
XmlUrlResolver resolver = new XmlUrlResolver( );
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
XslTransform transform = new XslTransform( );
transform.Load(@"..\PersonnelHTML.xsl",resolver);
transform.Transform(@"..\Personnel.xml",@"..\Personnel.html",resolver);
transform.Load(@"..\PersonnelCSV.xsl",resolver);
transform.Transform(@"..\Personnel.xml", @"..\Personnel.csv",resolver);
}
The Personnel.xml file contains the following items:
="1.0"="utf-8"
<Personnel>
<Employee name="Shahab" title="Customer Service" companyYears="1"/>
<Employee name="Noosha" title="Manager" companyYears="12"/>
<Employee name="NavidChas" title="Salesman" companyYears="3"/>
<Employee name="Mehrdad" title="CEO" companyYears="27"/>
<Personnel>
The PersonnelHTML.xsl stylesheet looks like this:
="1.0"="UTF-8"
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<html>
<head />
<body title="Personnel">
<xsl:for-each select="Personnel">
<p>
<xsl:for-each select="Employee">
<xsl:if test="position( )=1">
<table border="1">
<thead>
<tr>
<td>Employee Name</td>
<td>Employee Title</td>
<td>Years with Company</td>
</tr>
</thead>
<tbody>
<xsl:for-each select="../Employee">
<tr>
<td>
<xsl:for-each select="@name">
<xsl:value-of select="." />
</xsl:for-each>
</td>
<td>
<xsl:for-each select="@title">
<xsl:value-of select="." />
</xsl:for-each>
</td>
<td>
<xsl:for-each select="@companyYears">
<xsl:value-of select="." />
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:if>
</xsl:for-each>
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Here is the HTML source:
<html xmlns:xs="http://www.w3.org/2002/XMLSchema">
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body title="Personnel">
<p>
<table border="1">
<thead>
<tr>
<td>Employee Name</td>
<td>Employee Title</td>
<td>Years with Company</td>
</tr>
</thead>
<tbody>
<tr>
<td>Shahab</td>
<td>Customer Service</td>
<td>1</td>
</tr>
<tr>
<td>Noosha</td>
<td>Manager</td>
<td>12</td>
</tr>
<tr>
<td>Navid</td>
<td>Salesman</td>
<td>3</td>
</tr>
<tr>
<td>Mehrdad</td>
<td>CEO</td>
<td>27</td>
</tr>
</tbody>
</table>
</p>
</body>
</html>
The comma-delimited output is generated using PersonnelCSV.xsl and Personnel.xml; the stylesheet is shown here:
="1.0"="UTF-8"
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:
xs="http://www.w3.org/2002/XMLSchema">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:for-each select="Personnel">
<xsl:for-each select="Employee">
<xsl:for-each select="@name">
<xsl:value-of select="." />
</xsl:for-each>,<xsl:for-each select="@title">
<xsl:value-of select="." />
</xsl:for-each>,<xsl:for-each select="@companyYears">
<xsl:value-of select="." />
</xsl:for-each>
<xsl:text> 
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The output from the PersonnelCSV.xsl stylesheet is shown here:
Shahab,Customer Service,1
Noosha,Manager,12
Navid,Salesman,3
Mehrdad,CEO,27
History
- 22nd October, 2005: Initial post