Random photo
Loading...
Domains for sale
|
November 10, 2003"How to XSLT CSV file" revisitedThe idea is to represent non-XML formatted data as pure XML to be able to leverage many's favorite XML hammer - XSLT. I want to make it clear that approaching the problem this way doesn't abuse XSLT as XML transformation language. Non-XML data is being represented as XML and XSLT operates on it via XPath data model prism actually having no idea it was CSV file on the hard disk. Let's say what's given is this tab-delimited file, containing some info such as customer ID, name, address about some customers. You need to produce HTML report with customers grouped by country. How? Here's how: all you need is XmlCSVReader (cudos to Chris Lovett), XSLT stylesheet and couple lines of code to glue the solution: Code: using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.IO;
using Microsoft.Xml;
public class Sample {
public static void Main() {
//XMLCSVReader setup
XmlCsvReader reader = new XmlCsvReader();
reader.Href = "sample.txt";
reader.Delimiter = '\t';
reader.FirstRowHasColumnNames = true;
//Usual transform
XPathDocument doc = new XPathDocument(reader);
XslTransform xslt = new XslTransform();
xslt.Load("style.xsl");
StreamWriter sw = new StreamWriter("report.html");
xslt.Transform(doc, null, sw);
sw.Close();
}
}
XSLT stylesheet <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="countryKey" match="/*/*" use="country"/>
<xsl:template match="root">
<html>
<head>
<title>Our Customers Worldwide</title>
</head>
<body>
<table style="border:thin solid orange;">
<xsl:for-each select="*[count(.|key('countryKey',
country)[1])=1]">
<xsl:sort select="country"/>
<tr>
<th colspan="2"
style="text-align:center;color:blue;">
<xsl:value-of select="country"/>
</th>
</tr>
<tr>
<th>Customer Name</th>
<th>Account Number</th>
</tr>
<xsl:apply-templates
select="key('countryKey', country)"/>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="row">
<tr>
<xsl:if test="position() mod 2 = 1">
<xsl:attribute name="bgcolor">silver</xsl:attribute>
</xsl:if>
<td>
<xsl:value-of
select="concat(fname, ' ',mi, ' ', lname)"/>
</td>
<td>
<xsl:value-of select="account_num"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
Resulting HTML:
Main virtue of this approach is that all transformation and presentation logic is concentrated in only one place - XSLT stylesheet (add CSS according to your taste), C# code is fully agnostic about data being processed. In the same fashion CSV file can be queried using XQuery or XPath. Once the data is represented as XML, all doors are open. November 10, 2003 3:37 PM
| #.NET
Comments
mehrnaz, you better open connection to thge ftp server yourself and once you get response stream, feed it into XMLCSVReader. Posted by: Oleg Tkachenko at March 8, 2006 7:03 PMI am using XmlCsvreader to read csv files from the FTP server. You can use my CSV parser to speed up the parsing of the actual CSV file instead of using the class inside XmlCsvReader, http://www.csvreader.com/ . Posted by: shriop at July 16, 2005 5:25 PMDo you have .NET installed? I downloaded the XmlCsvReader. When I try to run it from ms-dos prompt window, my ms-dos prompt crashes, saying "tried to perform illegal opearation. Any idea as to how to fix this? Also is it possiible to run it from within MS Access visual basic? I will really appreciate any help with this. Best Regards, Ruslana Posted by: ateh at July 12, 2004 5:30 PMI don't think that can be done with xmlcsvreader (unless you extend it). Oleg, ----------- the Xml file I got using XmlCsvReader is :
---------- 1. How to add attributes to the elements? Any properties of xmlcsvreader to use? If so, what are they? Thanks for your help, Thanks Oleg, I got the dll and added the reference to my project. Best Rgds, Unfortunately XmlCSVReader doesn't provide dll you can refer to. So you need to do it yourself. Then you can a reference to the XmlCsvReader.dll into your project (no matter VB.NET or C#) and use Microsoft.Xml.XmlCsvReader class. Posted by: Oleg Tkachenko at April 28, 2004 10:31 AMI am not able to add 'Imports Microsoft.Xml'. For this to get, do I need to add any reference? If so, what reference should I add? Thanks for all your help. Best Rgds, Post a comment
|
||||||||||||||||||||||||||||||||||