Visualizzazione dei risultati da 1 a 4 su 4

Discussione: XPath

  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2000
    Messaggi
    1,175

    XPath

    Ho questo script javascript:
    codice:
    <script type="text/javascript">
    function loadXMLDoc(fname) {
        var xmlDoc;
        if (window.ActiveXObject) { // Internet Explorer
            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        } else if (document.implementation && document.implementation.createDocument) { // altri browser
            xmlDoc=document.implementation.createDocument("","",null);
        } else {
            alert('Non puoi utilizzare questo script!!!');
        }
    xmlDoc.async=false;
    xmlDoc.load(fname);
    return(xmlDoc);
    }
    
    /* funzione per utilizzare filtri XPath*/
    function myPath(xml, path) {
        var array = new Array();
        if (window.ActiveXObject) // Internet Explorer
            {
                xml.setProperty("SelectionLanguage","XPath");
                var nodes=xml.selectNodes(path);
                
                for (i=0;i<nodes.length;i++)
                    {
                        array[i] = nodes[i].childNodes[0].nodeValue;
                    }
                }
                // Mozilla, Firefox, Opera, etc.
                else if (document.implementation && document.implementation.createDocument)
                    {
                        var nodes=document.evaluate(path, xml, null, XPathResult.ANY_TYPE,null);
                        var result=nodes.iterateNext();
                        var j=0;
                        while (result)
                            {
                                array[j] = result.childNodes[0].nodeValue;  
                                result=nodes.iterateNext();
                                j++;
                            }
                    }
                        return array;
    }
    
            
    
    
    var xml = loadXMLDoc("rubriche.xml");
    titolo = myPath(xml, "/html/head/title");
    document.write(titolo);
    
    </script>
    che dovrebbe stamparmi il contenuto del documento caricato in loadXMLDoc, prima riga verde.
    Il documento ha questo formato:
    codice:
    <html> 
        <head> 
            <title>questo è un titolo</title>
        </head>
    </html>
    Premettendo che so che non è XHTML valido ne tantomeno XML valido, la mia domanda è un altra. Perchè se questo codice lo salvo come rubriche.xml mi ritorna il contenuto di title, mentre se questo stesso testo lo salvo come rubriche.html non mi viene stampato nulla???

  2. #2
    Utente di HTML.it
    Registrato dal
    Sep 2000
    Messaggi
    1,175
    up

  3. #3
    Domanda: con quale browser lo hai testato?

  4. #4
    Utente di HTML.it
    Registrato dal
    Sep 2000
    Messaggi
    1,175
    Safari 3 su Mac e Firefox 3 su Windows!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.