Javascript addParameter function (used for xslt) does not work

I am trying to request 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 simple:

 <?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 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 parameter. I use it in javascript to filter by numFoobar .

The problem is that Safari returns this error:

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

So why is addParameter not recognized?

Thanks,

Sincerely.

0
source share
3 answers

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

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

I could not find documentation for Opera (Presto), Safari and Chrome (Webkit). Feel free to edit this answer.

+3
source

Zakaria

Besides the error you mentioned (which apparently was addressed by @Alejandro), your <xsl:param name="numFoobar" /> not in a valid place.

In order for it to be a parameter of the stylesheet, it must be at the top level: it must appear in front of 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="/"> ... 
+1
source

Your XML file should have the following header:

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 a 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

  • XML Header <?xml-stylesheet href="yourFile.xsl" type="text/xsl"?>
  • Using Javascript setParemeter(null,numFoobar,'2');
  • XSLT adds your xsl:param at the top level, creating an XHTML file.
0
source

Source: https://habr.com/ru/post/1496358/


All Articles