Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Paul Hulatt 47 posts 59 karma points
    Aug 12, 2009 @ 14:42
    Paul Hulatt
    0

    Import Remote CSV in XSLT

    Does anyone know of an easy way that I can import a remote CSV file via XSLT?  I have tried using GetXmlDocumentByUrl but it (quite correctly) errors as the file being loaded is not an XML document.  IS there an equivalent for loading non-XML documents from remote locations.

    Cheers in advance

     

    Paul.

  • Bert 128 posts 251 karma points
    Aug 12, 2009 @ 14:45
    Bert
    0

    As far as I know, no.

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Aug 12, 2009 @ 14:48
    Thomas Höhler
    0

    Write your own xslt extension which loads the csv file into an XmlDocument and returens an XmlNodeIterator.

    If you have questions I can give you an example

    Thomas

  • Richard Soeteman 3722 posts 11521 karma points MVP
    Aug 12, 2009 @ 14:58
    Richard Soeteman
    0

    Hi Paul,

    GetXmlDocumentByUrl  is only for Xml. If you just want to import a csv file you could use umbImport. I've build a csv option, so you can select a csv file. This is not a remote file.

    If you need to access the file dynamicly. You can create an xslt extension that returns the data as an xpathNodeIterator.

    Hope this helps you,

     

    Richard

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Aug 12, 2009 @ 14:59
    Thomas Höhler
    1

    Something like that will do it:

    public static XPathNodeIterator GetCsv(string filename)
    {
    XmlDocument xdoc = new XmlDocument();
    XmlElement rows = xdoc.CreateElement("rows");
    xdoc.AppendChild(rows);

    using (System.IO.StreamReader sr = new System.IO.StreamReader(filename))
    {
    String line;
    // Read and display lines from the file until the end of
    // the file is reached.
    while ((line = sr.ReadLine()) != null)
    {
    XmlElement row = xdoc.CreateElement("row");
    rows.AppendChild(row);
    foreach (var item in line.Split(";".ToCharArray()))
    {
    XmlElement col = xdoc.CreateElement("col");
    col.Value = item;
    row.AppendChild(col);
    }
    }
    }
    XPathNavigator xp = xdoc.CreateNavigator();
    return xp.Select("/rows");
    }

    hth, Thomas

  • Lee Kelleher 3890 posts 14696 karma points MVP 9x admin c-trib
    Aug 12, 2009 @ 15:19
    Lee Kelleher
    101

    Following on from Thomas' reply, (at present) a custom method is the only way to go.

    If you need a quick-and-ready solution to this, and don't want to get involved with Visual Studio (God knows why? We all love it), you can do this all natively in XSLT ... using a custom function (see Extend your XSLT with custom functions wiki page)

    Here's an example:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    xmlns:bodenko="urn:bodenko"
    exclude-result-prefixes="msxml umbraco.library bodenko">
    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <msxml:script implements-prefix="bodenko" language="C#">
    <msxml:assembly name="System" />
    <msxml:assembly name="umbraco" />
    <msxml:using namespace="System.Net" />
    <msxml:using namespace="umbraco" />
    <![CDATA[
    public static XPathNodeIterator GetCsvByUrl(string url, string separator)
    {
    String csv = String.Empty;
    using (WebClient client = new WebClient())
    {
    csv = client.DownloadString(url);
    }
    return umbraco.library.Split(csv, separator);
    }
    ]]>
    </msxml:script>

    <xsl:template match="/">
    <xsl:variable name="csv" select="bodenko:GetCsvByUrl('http://www.domain.com/filename.csv', ',')" />
    <xsl:if test="count($csv/value) &gt; 0">
    <ul>
    <xsl:for-each select="$csv/value">
    <li>
    <xsl:value-of select="." />
    </li>
    </xsl:for-each>
    </ul>
    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>

    The "GetCsvByUrl" method doesn't take multi-line (rows) into account... you can quite easily replace this with Thomas' method (above).

  • Paul Hulatt 47 posts 59 karma points
    Aug 12, 2009 @ 16:29
    Paul Hulatt
    0

    Cheers Lee, that was just the sort of start I was hoping for now I can concentrate of dealing with the actual data and displaying it in some kind on meaningful way.

    Richard, thanks for your reply regarding umbImport, I've used your fantastic package already when migrating an existing site over to Umbraco so thanks for all your hard work.

    Paul

Please Sign in or register to post replies

Write your reply to:

Draft