0

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
  • @Alejandro: Thanks man! Please, add your comment as an answer so that I can accept it . Commented Nov 5, 2010 at 10:03

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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="/">
    ...

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.