Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    xml, xsl and javascript loop not working, plz help

    since a week i have been search online, but no solution i found.

    i have 3 files, XML,dtd and xsl.
    i have a button click Event, in which, i have to read the XML file inside the JavaScript and Export as a csv file.
    i am trying to find the solution but ist not working
    if i try the solution in html file it is working, but if i use in XML/xsl it is not working
    Problem 1. xmlobj value is null, if i use in XML/xsl
    Problem 2. the Loop is not working due to less than Symbol, i also tried xsl tags CDATA tags but of no use.



    1. bclist.xml
    -----------
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE bclist SYSTEM "bclist.dtd">
    <?xml-stylesheet type="text/xsl" href="bclist.xsl" ?>
    <bclist>
    <creation>
    <userinitials>andrew </userinitials>
    </creation>
    <line no="1">
    <itemid number="20">1</itemid>
    <date>01.01.1000</date>
    <time>00:00</time>
    </line>
    <line no="2">
    <itemid number="30">2</itemid>
    <date>01.01.2000</date>
    <time>00:10</time>
    </line>
    </bclist>

    2. bclist.dtd
    ----------
    <!ELEMENT barcodelist (#PCDATA)>

    3. bclist.xsl
    -----------
    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:decimal-format name="de" decimal-separator="," grouping-separator="."/>
    <xsl:key name="eventid" match="/bclist/line" use="itemid/@number" />

    <xsl:template match="/">
    <html>
    <head>
    <script language="Javascript" >
    function csvExport(xmlfile)
    {
    var xmlDaten = new ActiveXObject("Microsoft.XMLDOM");
    xmlDaten.async = false;
    xmlDaten.load(xmlfile);
    var xmlobj=xmlDaten.documentElement;
    var fs;
    fs = new ActiveXObject("Scripting.FileSystemObject");
    var csvfile= xmlfile.replace(".xml",".csv");
    csvfile=location.href.substring(8, location.href.lastIndexOf("/")+1)+csvfile;
    var s = fs.CreateTextFile(csvfile,true);
    s.write("name,itemid_number,itemid,date,time");
    s.write("\r\n");
    var i=1;
    var ncount=xmlobj.childNodes.length-1;
    //while(i<3)

    while(2>i)
    {
    var node1=xmlobj.childNodes[0];
    s.write(node1.childNodes[0].nodeTypedValue+",");
    var node2=xmlobj.childNodes[i];
    var node20=node2.firstChild;
    s.write(node20.getAttribute("number")+",");
    s.write(node2.childNodes[0].nodeTypedValue+",");
    s.write(node2.childNodes[1].nodeTypedValue+",");
    s.write(node2.childNodes[2].nodeTypedValue+";");
    s.write("\r\n");
    i++
    }
    s.Close();
    }
    </script>

    <style type="text/css">
    body {
    font-family:verdana,helvetica,arial,sans-serif;
    }
    table {
    font-size:10pt;
    border-collapse:collapse;
    }
    .titel {
    font-size:14pt;
    font-weight:bold;
    }
    td {
    width:70px;
    text-align: right;
    }
    td.short {
    width:30px;
    }
    td.middle {
    width:90px;
    text-align: right;
    }
    td.long {
    width:300px;
    }
    td.summe {
    border-style: inset none none none;
    }
    td.deli {
    border-style: none inset none none;
    }
    td.summedeli {
    border-style: inset inset none none;
    }
    td.summeall {
    border-style: inset inset inset none;
    }
    td.delispace {
    border-style: none inset none none;
    }
    th {
    border-style: none none inset none;
    text-align: center;
    }
    </style>
    </head>
    <body id="start">
    <p class="titel">
    <xsl:variable name="xmlfile" select="/bclist/creation/XMLFileName" />
    <input onClick="window.location.reload();" type="button" value="Ticketliste" />
    <input onClick="csvExport('{$xmlfile}')" type="button" value="CSV export" />
    </p>
    <div id="gerendert">

    <b>
    <xsl:value-of select="count(/bclist/line/itemid)" />
    </b><br /><br />

    <table cellpadding="3" bgcolor="#E0E0E0">
    <tr>
    <th align="right">date</th>
    <th align="right">time</th>
    </tr>

    <xsl:for-each select="bclist/line/itemid">
    <xsl:variable name="date" select="../date" />
    <xsl:variable name="time" select="../time" />
    <tr>
    <td class="short" align="right">
    <xsl:value-of select="$date" />
    </td>
    <td class="short" align="right">
    <xsl:value-of select="$time" />
    </td>
    </tr>
    </xsl:for-each>
    </table>
    <br />
    <br />
    </div>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,402
    Thanks
    11
    Thanked 595 Times in 575 Posts
    couple things i noticed:
    1. you don't need a doctype in your data file, in fact it might stop it from working fully when not using the quirk-tolerant embedded stylesheet method.
    2. you don't need a dtd
    3. you need to use a cdata wrap in the script tags on your xsl file.
    4. you can't call CreateTextFile from a http:// web page, and you can't use XSL from a file://, so something has to give.
    you can use my download.js to save the csv file on the client, but that doesn't work in IE until version 10.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #3
    Regular Coder
    Join Date
    Jan 2013
    Location
    Sunnyvale, CA
    Posts
    115
    Thanks
    8
    Thanked 7 Times in 7 Posts
    1. Your DTD file does not describe your XML file, and needs to be modified.

    2. You don't require JavaScript to transform your XML file's data into CSV format, all that you require is the XML source file and an XSL file that parses it and presents it as CSV. rnd me is most certainly correct: You do not require a DTD to transform XML into CSV.

    DTD, XML and XSL are pretty easy to learn. zvon.org offers some great tutorials that will help you with your work: tutorial @ZVON.org

    Good luck, and feel free to post questions as they come up


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •