dkumarx / ajaxslt

Automatically exported from code.google.com/p/ajaxslt
Other
0 stars 0 forks source link

Patch to support parameters #16

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Change the 'xsltProcess' function to the following to support passing a 
hash of parameters as third parameter to this function:

function xsltProcess(xmlDoc, stylesheet, params) {
  var output = domCreateDocumentFragment(new XDocument);
  var c = new ExprContext(xmlDoc);
  if(params) {
    for(var p in params)
      c.setVariable(p, new StringValue(params[p]));
  }
  xsltProcessContext(c, stylesheet, output, params);
  var ret = xmlText(output);
  return ret;
}

Original issue reported on code.google.com by rst...@gmail.com on 23 Oct 2007 at 12:01

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
To support good values, we could change the for-loop to be:

    for(var p in params) {
      var v;
      var pp = params[p];
      switch(pp) {
        case 'true':
          v = new BooleanValue(true);
          break;
        case 'false':
          v = new BooleanValue(false);
          break;
        default:
          if(/^[0-9]+$/.test(pp))
            v = new NumberValue(parseInt(pp));
          else
            v = new StringValue(pp);
          break;
      }
      c.setVariable(p, v);
    }

Original comment by rst...@gmail.com on 23 Oct 2007 at 12:14

GoogleCodeExporter commented 9 years ago
I like the idea of this patch, but I'm a bit worried about it, because it uses 
"for
(var p in params)", which may not work if Object.prototype has been modified.  
We
could workaround using .hasOwnProperty, but AJAXSLT is meant for browsers with 
very
limited JS support, so we can't be sure we'll be able to use .hasOwnProperty.  
[IMO,
there's no good implementation of an associative array with key iterators 
available
to us.]

In the latest checked in version of xpath.js, ExprContext.setVariable now more
naturally supports passing in native JS values; it will convert them into XPath
values on-the-fly.

Instead of creating a new Object(), filling it up with values, and passing it to
xsltProcess, you can/should create a new ExprContext and call setVariable on it 
for
all the parameters you want to pass in.  Then your can call xsltProcessContext 
with
the context you created.  That should be safer in general.

Original comment by DanFabul...@gmail.com on 7 Nov 2007 at 4:47