RE: [xsl] passing values from JavaScript to XSL using MSXML3

Subject: RE: [xsl] passing values from JavaScript to XSL using MSXML3
From: "bryan" <bry@xxxxxxxxxx>
Date: Mon, 21 Jul 2003 10:54:20 +0200
This question comes up sometimes, as a general rule what one wants is
not javascript/xslt integration but to generate a javascript from xslt,
true javascript/xslt integration, in the manner of having an extension
function written in javascript wherein the values of said function could
be dynamically written to etc.  would be in most instances a bad idea,
however as many bad ideas it is doable. I did a proof of concept about a
month and a half ago that I haven't had any time, nor really
inclination, to extend given that I think it a bad idea. 

Here is what I did (note that this was a trial run hence the dirty,
slightly redundant code)-

The xslt:

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
	xmlns:msxsl="urn:schemas-microsoft-com:xslt"  
	xmlns:jsc="http://www.somefakeurl/jsc";
	xmlns:js="http://www.somefakeurl/jscriptEvalExtension";
	version="1.0"
	 extension-element-prefixes="msxsl xsl js jsc"
>
<xsl:output method="xml" encoding="UTF-8"/>


<msxsl:script language="JScript" implements-prefix="jsc">

var valparam;
var tempVal;
var procholder;

	function jseval(value){ 

		valparam = eval(value);
 		return valparam;
  	}

	function jsevalErr(value){

		valparam = eval(value);
			if(typeof valparam == "string"){
			procholder = value;
			}
			else{
			valparam = "error%%valparam not string:: is"  +
typeof 			valparam;
			}
			return valparam;


			}

			function jsGetVar(value){
			var stempVal;
			if (value = "valparam"){
			tempVal = valparam;
			stempVal = new String(valparam);
			stempVal = String(stempVal);
			}
			else{
			stempVal = stempVal;
			}

			if(typeof stempVal == "string"){
			return stempVal;

			}
			else{
			var newstring;

			newstring = "error%% tempval not evaluate to
string is " + 			typeof stempVal;
			return newstring;

			}

			}
	function jsAddVar(oldvalue,addedvalue){
		if(oldvalue = "valparam"){
			if(typeof valparam == "string"){
			valparam = valparam + eval(addedvalue);
			return valparam;
			}
			else{
			String(valparam);
			valparam = addedvalue;
			return valparam;

			}
		}
		else{

			if(typeof tempVal == "string"){
			tempVal = addedvalue;
			tempVal = eval(tempVal);
			return tempVal;
			}
			else{
			String(tempVal);
			tempVal = tempVal;
			return tempVal;

			}

		}

	}

	function jsgetProc(){
	return procholder;

	}

</msxsl:script>
<xsl:template match="*">
<xsl:copy><xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>


<xsl:template match="js:runStraight">
<eval>
<xsl:value-of select="jsc:jsevalErr(string(.))"/>
</eval>
</xsl:template>

<xsl:template match="js:getJsVariable">
<varValue name="{@name}">
<xsl:value-of select="jsc:jsGetVar(string(@name))"/>
</varValue>
</xsl:template>

<xsl:template match="js:addToVariable">
<added to="{@name}">
<varValue name="{@name}">
<xsl:value-of select="."/>
</varValue>
<eval>
<xsl:value-of select="jsc:jsAddVar(string(@name),string(.))"/>
</eval>
</added>
</xsl:template>

<xsl:template match="js:getlastProc">
	<lastproc>
	<value>
       <xsl:value-of select="jsc:jsgetProc()"/>
       </value>
	  <xsl:if test="@eval = 'true'">
 		<eval>
            <xsl:value-of select="jsc:jsevalErr(jsc:jsgetProc())"/>
            </eval>
   	  </xsl:if>
</lastproc>
</xsl:template>
</xsl:stylesheet>



when this stylesheet is run against the following xml:

<?xml version="1.0"?>
<test xmlns:js="http://www.somefakeurl/jscriptEvalExtension";>
<example>
<desc>send the string from js:runStraight to an eval function, returns
string</desc>
<js:runStraight>var blah;blah = &quot;blah&quot;;if(blah =
&quot;blah&quot;){
blah = &quot;some string&quot;
}</js:runStraight>
</example>
<p> the next tag gets the value of valparam which was set by the former
tag. </p>

<js:getJsVariable name="valparam"/>

<js:runStraight>var blah; blah= &quot;new blah&quot;;</js:runStraight>

<js:getJsVariable name="tempVal"/>

<js:addToVariable name="valparam">var blah = &quot;this is added&quot;;
blah = " " + blah;
</js:addToVariable>

<js:getJsVariable name="valparam"/>

<js:getlastProc/>

</test>


it gives the following result:

<?xml version="1.0" encoding="UTF-8"?>
<test xmlns:js="http://www.somefakeurl/jscriptEvalExtension";>
<example>
<desc>send the string from js:runStraight to an eval function, returns
string</desc>

<eval>some string</eval></example>

<p> the next tag gets the value of valparam which was set by the former
tag. </p>

<varValue name="valparam">some string</varValue>

<eval>new blah</eval>

<varValue name="tempVal">new blah</varValue>

<added to="valparam">
<varValue name="valparam">var blah = "this is added";
blah = " " + blah;
</varValue>
<eval>new blah this is added</eval></added>
<varValue name="valparam">new blah this is added</varValue>
<lastproc><value>var blah; blah= "new blah";</value></lastproc>
</test>

Obviously primitive but it does show that it is possible to pass values
back and forth between jscript and xslt in msxml. 

If anyone thinks this is not totally useless and can see some
possibilities I would like to hear about them, as it might inspire me to
actually build something with this (true, I can see some possibilities
also, but I'd like to see if anyone sees the same as I do, or if I've
just finally gone off the deep end).

Also: Sergey - you're marking up the Mabinogion? Any other literature
you've got? Is it possible to get the final documents from you?


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread