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 2 of 2
  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,366
    Thanks
    11
    Thanked 590 Times in 571 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 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    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
    •