Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2004
    Messaggi
    11

    Problema Javascript + XSL

    Ho provato in tutti i modi a passare una variabile dichiarata in JavaScript al file xsl ed ho trovato un codice, che dice di creare un parametro con XSL (param name=...)

    Tutto questo perchè mi serviva una variabile che si trovava nel referrer ed in JavaScript sò come estrarla mentre con XSL no.

    Ho seguito tutto il discorso l'ho implementato ma ora ricevo un errore JavaScript:

    the stylesheet does not contain a document element, or the Xml file is not well formed.

    File.Htm
    codice:
    <script language="JavaScript">
    //JavaScript
    urlstring = location.href;
    qrystring=urlstring.substring(urlstring.indexOf("=") + 1, urlstring.length);
    alert(qrystring);
    //Ambiente per Impostazione Variabili per XSL
    var xml = new ActiveXObject("MSXML2.DomDocument.4.0");
      xml.async = false;
      xml.load("DBFiabe.xml");
    
     var xsl = new ActiveXObject("MSXML2.FreeThreadedDomDocument.4.0");
      xsl.async = false;
      xsl.load("DBFiabe.xsl");
    
      var template = new ActiveXObject("MSXML2.XSLTemplate.4.0")
      template.stylesheet = xsl
      processor = template.createProcessor()
    
      processor.input = xml
      processor.addParameter("param1", qrystring)
    
    
      processor.transform()
    
      document.open()
      document.write(processor.output)
      document.close()
    </script>
    File Xsl
    codice:
    <?xml version="1.0" encoding="UTF-8" ?>
    <stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="param1"/>
    <xsl:template match="/">
    <html>
    <head>
    <title>Fiaba</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <xsl:apply-templates/>
    </body>
    </html>
    </xsl:template>
    <xsl:template match="DBFiabe">
     <xsl:for-each select="//fiaba">
    <xsl:if test="@number=$param1">
     <xsl:value-of select="body"/>
     </xsl:if>
     </xsl:for-each>
    </xsl:template>
    </stylesheet>
    File Xml
    codice:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <DBFiabe>
    <fiaba number="1">
    <title>Ma bella addormentata</title>
    <audio></audio>
    <body>ilhiohòoilhlòkhjklhkòjhoòihkj.hòklhkòhkjhkjhjhvg hv yhgjhghjgjhgu
    ukhukhikhkuhiuhuiob
    klnjlknkln</body>
    </fiaba>
    <fiaba number="2">
    <title>La pendicana: Monica</title>
    <audio></audio>
    <body></body>
    </fiaba>
    <fiaba number="3">
    <title></title>
    <audio></audio>
    <body></body>
    </fiaba>
    </DBFiabe>

  2. #2
    Utente di HTML.it
    Registrato dal
    Jan 2004
    Messaggi
    11
    Ragazzi aiutatemi a risolvere questo problemino! Per favore, vi ho messo i tre file in modo tale da poterlo provare direttamente, se funzia

    Ho provato anche in un altro modo, cioè utilizzando il namespace <msxml:script...>
    ma il problema è che non posso invocare l'oggetto location, che mi è fondamentale.

    Se sapete come passare una variabile da JavaScript a XSL in altri modi fatemelo sapere.


  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2004
    Messaggi
    11
    Ragazzi all'inizio ho detto che mi serve una variabile che si trova nel referrer ma volevo dire query string, per passare la variabile (querystring) creo una variabile XSL param1, che poi richiamo nel codice XSL con $param1.

    Quello che ho trovato per implementare questo tipo di passaggio di variabili ve lo listo qui sotto:


    Hi,

    I think you mean you want to pass in a parameter from a javascript function,
    if so try:

    (this is assuming you have msxml3 installed)
    codice:
    function whatever(){
    
      var xml = new ActiveXObject("MSXML2.DomDocument.3.0");
      xml.async = false;
      xml.load("yourXmlFile.xml");
    
      var xsl = new ActiveXObject("MSXML2.FreeThreadedDomDocument.3.0");
      xsl.async = false;
      xsl.load("yourXslFile.xsl");
    
      var template = new ActiveXObject("MSXML2.XSLTemplate")
      template.stylesheet = xsl
      processor = template.createProcessor()
    
      processor.input = xml
      processor.addParameter("param1", "param1value")
      processor.addParameter("param2", "someOtherValue")
      //and so on
    
      processor.transform()
    
      document.open()
      document.write(processor.output)
      document.close()
    }
    Make sure you include the following param declarations as top-level elements
    (basically below the namespace) of your stylesheet:
    codice:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:param name="param1"/>
    <xsl:param name="param2"/>
    You can then access the params using $param1 and so on...

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 © 2026 vBulletin Solutions, Inc. All rights reserved.