简体   繁体   中英

Load an xml file with javascript in XSLT

I am stuck at this. I have an XSLT file that uses an XML file to retrieve data from it. IN this XSLT file is also a javascript. It's purpose is to create counters to the 'for each' statement in the XSLT.

Now I need to add a feature to the javascript that gets a specific entry from the XML file. I will paste a part of the xml here so I can explain further.

<ROW>
    <Date>03-12-2013</Date>
    <School>SvR</School>
    <Locale>B1.04</Locale>
    <Class>1236VUGK16</Class>
    <Time>09:00-16:00</Time>
</ROW>

I need to get the data from "Date". But I do not know how to load an xml file in javascript to achieve this. I've found some html codes that tell me to xmlDoc.load will do this, but it does not. It will give me a syntax error. Is this because it is in XSLT? How can I achieve what I want?

you need 4 things

  1. data.xml -> XML file
  2. Openxml.js -> contains funciton to open Javascript
  3. filejs.js -> contains function to read/write and others things with data.
  4. index.html -> show information

part 1, your xml file:

<?xml version="1.0" encoding="UTF-8"?>
<events>
<ROW>
<Date>03-12-2013</Date>
    <School>SvR</School>
    <Locale>B1.04</Locale>
    <Class>1236VUGK16</Class>
    <Time>09:00-16:00</Time>
</ROW>
</events>

part 2:

unction OpenFile(fichXML)
{
        var xmlDoc=undefined;
        try
        {
            if (document.all) //IE
            {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            }
            else //firefox
            {
                xmlDoc = document.implementation.createDocument("","",null);
            }
            xmlDoc.async=false;
            xmlDoc.load(fichXML);


        }
        catch(e)
        {
            try { //otros safari, chrome
                    var xmlhttp = new window.XMLHttpRequest();
                    xmlhttp.open("GET",fichXML,false);
                    xmlhttp.send(null);
                    xmlDoc = xmlhttp.responseXML.documentElement;
                    return xmlDoc;
            } 
            catch (e) 
            {
                return undefined;
            }

        }
        return xmlDoc;
}

part 3:

    function Event(date)
    {
        this.date = date;

    }   
    //funciton to charge XMLElements
     function ChargeXMLElements()
        {
            try
            {
                xmlDoc=OpenFile("data.xml");
                eventosXML=xmlDoc.getElementsByTagName('ROW');

                if (eventosXML.length>0)
                {
                    eventos=new Array(); //clase con los datos cargados
                 }
                for(var i=0; i< eventosXML.length; i++) 
                {
                    xmlEvento=eventosXML[i];

                    fecha=xmlEvento.getElementsByTagName("DATE")[0].firstChild.nodeValue;

                    evento = new Evento(fecha);       
                    eventos.push(evento);

                }
                return eventos;
            }
            catch(e)
            {
                alert("Error on the load data");
            }    

        }
//show information take on previously function, and show on a table (with only 1 column)
function showinformation() 
{
        var tr;
        var td;
        var tabla;
        var l=0;
        ev= CargarXMLEventos();
        auxUnEven=ev.pop();
        tabla=document.getElementById("datos");
        while (auxUnEven!=undefined)
        {
            tr=tabla.insertRow(l);
            //creamos las columnas de la tabla 
            td=tr.insertCell(0);
            td.innerHTML=auxUnEven.date;
            /*td=tr.insertCell(1);
            td.innerHTML=auxUnEven.hora;
            td=tr.insertCell(2);
            td.innerHTML=auxUnEven.comentario;   */ //if you need more columns         
            l++;
            auxUnEven=ev.pop();
        }
        if (l==0)
        {
            tabla=document.getElementById("datos");
            tr=tabla.insertRow(l);
            td=tr.insertCell(0);
            td.innerHTML=" dont have events ";
        }

}

part 4:

<!DOCTYPE html
 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>example XML</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="filejs.js"></script>
    <script type="text/javascript" src="Openxml.js"></script>
  </head>
<body>
    <table id="datos">
        <script type="text/javascript">
            showinformation();
        </script>
    </table>
</body>
</html> 

i think that is functional but i don't know. I see the information here: http://elcaminillo.wordpress.com/2012/01/15/leer-xml-en-javascript/ (spanish, sorry)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM