addParameter javascript (used for xslt) function d

2019-06-11 09:54发布

问题:

I'm trying to query an xml file using xslt called by javascript.

Here is the main page:

<html>
 <head>
 <script>
 function loadXMLDoc(dname)
 {
 if (window.XMLHttpRequest)
   {
   xhttp=new XMLHttpRequest();
   }
 else
   {
   xhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
 xhttp.open("GET",dname,false);
 xhttp.send("");
 return xhttp.responseXML;
 }

 function displayResult()
 {
 xml=loadXMLDoc("FooBar.xml");
 xsl=loadXMLDoc("FooBar.xsl");
 // code for IE
 if (window.ActiveXObject)
   {
   ex=xml.transformNode(xsl);
   document.getElementById("Main").innerHTML=ex;
   }
 // code for Mozilla, Firefox, Opera, etc.
 else if (document.implementation && document.implementation.createDocument)
   {
   xsltProcessor=new XSLTProcessor();
   xsltProcessor.addParameter("numFoobar","2");
   xsltProcessor.importStylesheet(xsl);

   resultDocument = xsltProcessor.transformToFragment(xml,document);
   document.getElementById("Main").appendChild(resultDocument);
   }
 }
 </script>
 </head>
 <body onload="displayResult()">
 <div id="Main">
 </div>
 </body>
</html>

The xml file is a simple one:

<?xml version="1.0" standalone="yes"?>
<Foobars xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <Foobar>
    <numFoobar>1</numFoobar>
    <nameFoobar>Foo</numFoobar>
 </Foobar>
<Foobar>
    <numFoobar>2r</numFoobar>
    <nameFoobar>Bar</nameFoobar>
 </Foobar>
</Foobars>

and for the xslt :

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <table border="2" bgcolor="yellow">
      <tr>
        <th>Num</th>
        <th>Name</th>
      </tr>
      <xsl:param name="numFoobar"
      <xsl:for-each select="Foobars/Foobar">
      <tr>
        <td><xsl:value-of select="numFoobar"/></td>
        <td><xsl:value-of select="nameFoobar"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

As you can see, in the xslt file I added a parametere. I use it in the javascript to filter by numFoobar.

The problem is that the Safari returns that error:

TypeError: Result of expression 'xsltProcessor.addParameter' [undefined] is not a function.

So, why the addParameter is not recognized?

Thank you,

Regards.

回答1:

From https://developer.mozilla.org/en/The_XSLT/JavaScript_Interface_in_Gecko:Setting_Parameters

XSLT provides the xsl:param element, which is a child of the xsl:stylesheet element. XSLTProcessor() provides three JavaScript methods to interact with these parameters: setParameter, getParameter and removeParameter. They all take as the first argument the namespace URI of the xsl:param (Usually the param will fall in the default namespace, so passing in "null" will suffice.) The local name of the xsl:param is the second argument. setParameter requires a third argument - namely the value to which the parameter will be set.

I couldn't find documentation for Opera (Presto), or Safari and Chrome (Webkit). Feel free to edit this answer.



回答2:

Zakaria,

Aside from the error you mentioned (which @Alejandro has apparently addressed), your <xsl:param name="numFoobar" /> is not in a valid location.

In order for it to be a stylesheet parameter, it needs to be at the top level: it must come before all templates, under <xsl:stylesheet>:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="numFoobar" />
  <xsl:template match="/">
    ...


回答3:

You must have the following heading on your XML file:

XML yourFile.XML

<?xml version="1.0"  encoding="UTF-8"?>
 <?xml-stylesheet href="yourFile.xsl" type="text/xsl"?>

Javascript HTML yourFile.html here I used myParam as variable

else if (document.implementation && document.implementation.createDocument)

{ xsltProcessor = new XSLTProcessor(); xsltProcessor.importStylesheet(xsl);
xsltProcessor.setParameter(null, "numFoobar", "2"); resultDocument = xsltProcessor.transformToFragment(xml, document); document.getElementById("transformResult").appendChild(resultDocument); }

XSLT finally yourFile.XLST

<xsl:output method="html"/> 
 <xsl:param name="numFoobar" /> 
 <xsl:template match="/">
 <xsl:for-each select="Foobars/Foobar">  
 <tr>
     <td><xsl:value-of select="$numFoobar"/></td>

In three steps

  1. XML heading <?xml-stylesheet href="yourFile.xsl" type="text/xsl"?>
  2. Javascript using setParemeter(null,numFoobar,'2');
  3. XSLT add your xsl:param at top level generating the XHTML file.