How to access javascript variable inside xslt

I have the following code

<xsl:result-document href="output1/output3/index.html" format="html"> <html> <head> <SCRIPT LANGUAGE="JavaScript"> function getParams() { var idx = document.URL.indexOf('?'); var params = new Array(); if (idx != -1) { var pairs = document.URL.substring(idx+1, document.URL.length).split('&amp;'); for (var i=0; i&lt;pairs.length; i++) { nameVal = pairs[i].split('='); params[nameVal[0]] = nameVal[1]; } } return params; } params = getParams(); </SCRIPT> <SCRIPT LANGUAGE="JavaScript"> val = unescape(params["value"]); document.write("value = " + val); </SCRIPT> </head> <body> <xsl:choose> <xsl:when test="a:id=''"> <xsl:value-of select="a:name"/> </xsl:when> </xsl:choose> </body> </html> </xsl:result-document> 

I want to access the javascript variable 'val' in xsl: when '. I need a value so that I can run the condition. Any ideas - How to do this?

+4
source share
3 answers

Saxon-CE is an XSLT 2.0 processor that runs in a browser. It is compiled for JavaScript and provides a high level of JavaScript compatibility. JavaScript nodes, DOMs, numbers, strings, booleans, and arrays (converted to XSLT 2.0 sequences) work relatively easily through the JavaScript / XSLT interface.

In your particular case, you have various options in Saxon-CE; you can directly call an existing or entered JavaScript function using ixsl: call (), or you can use ixsl: get () to get the value of a variable. Or you can do it all from your XSLT 2.0:

[Edited with corrections mentioned in the comment]

XSLT Code

Here the same code as the text instead of the graphic - without formatting

 <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ixsl="http://saxonica.com/ns/interactiveXSLT" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:f="urn:local-function" xmlns:a="urn:source-xml" extension-element-prefixes="ixsl" version="2.0" > <xsl:template match="/"> <xsl:variable name="urlparams" select="ixsl:get( ixsl:get(ixsl:window(),'location'), 'search')" as="xs:string"/> <xsl:variable name ="pairs" select="tokenize(substring($urlparams,2), '&amp;')" as="xs:string*"/> <xsl:variable name="value" select="f:getValue($pairs)"/> <p>value: <xsl:value-of select="$value"/></p> <xsl:choose> <xsl:when test="a:id eq $value"> <xsl:value-of select="a:name"/> </xsl:when> </xsl:choose> </xsl:template> <xsl:function name="f:getValue" as="xs:string*"> <xsl:param name="pairs" as="xs:string*"/> <xsl:sequence select="for $p in $pairs, $v in substring-before($p, '=') return if ($v eq 'value') then substring($p, string-length($v) + 2) else ()"/> </xsl:function> </xsl:transform> 
0
source

Various XSLT mechanisms allow javascript to be executed in its conversion loop. However, you simply include the script as part of the ouptut markup (i.e., just plain JS).

See the IBM example. They have special tags for the content and execution of JavaScript, they will be different for each engine. Note that they use a function to return a value from a JavaScript fragment, if direct access to the variable is not available, write a function to return the value.

+1
source

What you are asking is impossible: JavaScript is executed by the browser when rendering the page - and there is no way around this because JavaScript parses the URL of the page that only the browser will know.

XSLT, on the other hand, generates an HTML page - therefore, it is executed before the browser even sees that JavaScript is executing.

0
source

All Articles