Re: [xsl] document('data.csv') fails; workaround needed

Subject: Re: [xsl] document('data.csv') fails; workaround needed
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 22 Nov 2001 14:19:06 +0000
Hi Dan,

> Is there a clever way to import CSV data into the stylesheet? The
> following fails of course:
>   <xsl:variable name="csv" select="document('data.csv')"/>
> I can't alter the file data.csv but need the data in it for a helper
> lookup function. Thanks in advance.

If data.csv stays the same, then convert it to an XML file using
sed or perl or whatever you fancy, and use that.

If data.csv changes, but doesn't contain any <s or &s, then you could
create a wrapper XML document that accesses it as an external entity -

<?xml version="1.0"?>
<!DOCTYPE data [
<!ENTITY data SYSTEM 'data.csv'>

You could then access the data using the document() function, though
you'd have to work through the CSV document using string manipulation
functions in XSLT/XPath, which isn't all that fun.

Alternatively, if you're using an XSLT processor that accepts SAX
events you could write a custom entity resolver that reads in the CSV
document and generates SAX events to make the XSLT processor think
that it's accessed an XML document. That would also allow you take
advantage of the string manipulation support in whatever programming
language you were using, such that the XSLT processor 'sees' the CSV
file as elements.



Jeni Tennison

 XSL-List info and archive:

Current Thread