5

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 the value so that I can run the condition. Any ideas - How to do it?

3
  • When/how is the transform occurring? Commented Jun 8, 2012 at 10:23
  • It is impossible. Try to explain what exactly do you want to achieve with it, and we'll see if there is some other way. Commented Jun 8, 2012 at 10:23
  • I am having the Javascript code to get the value from the url. I am able to get the value from the url and print it through JS function. Now I need this val = unescape(params["value"]); to be used in XSL <xsl:when test="a:id='???'"> in place of question marks. Commented Jun 8, 2012 at 10:27

3 Answers 3

1

Saxon-CE is an XSLT 2.0 processor that runs in the browser. It's compiled to JavaScript and provides a high level of JavaScript interoperability. JavaScript/DOM nodes, numbers, strings, boolean values and arrays (converted to XSLT 2.0 sequences) work relatively seamlessly across the JavaScript/XSLT interface.

For your specific case you have various options in Saxon-CE; you can directly call an existing or injected JavaScript function using ixsl:call() or you can use ixsl:get() to retrieve the value of a variable. Or, you can do the whole thing from within your XSLT 2.0:

[Edited with corrections mentioned in comment]

XSLT Code

Here's the same code included as text instead of a 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>
Sign up to request clarification or add additional context in comments.

3 Comments

The answer's incomplete because you supplied no sample input but I was hoping you could adjust the code based on this rough sample. There's an error also because string-length($v) + 1 should be string-length($v) + 2 - and finally, 'location.search' is unsupported until the next release - you'll need to use ixsl:get on 'location' and then again on 'search' - using location as the context object this time.
it gives me an error - Cannot find a matching 0-argument function named {saxonica.com/ns/interactiveXSLT}window()
You need to use Saxon-CE for this - the error suggests another XSLT processor is being used that doesn't support this extension function. Are you using Saxon-CE?
1

Various XSLT engines allow the execution of javascript in their transformation cycle. However you're just including the script as part of the ouptut markup (i.e. just regular JS).

See IBM's example. They have special tags for containing and executing JavaScript, it will differ per engine. Note that they use a function to return a value from a piece of JavaScript, if direct variable access isn't possible, write a function to return the value.

1 Comment

i saw this link but I am unable to apply this in my code. Can you help me in how to write that function.
0

What you are asking is impossible: the JavaScript is executed by the browser when the page is rendered - and there are no way around that, because the JavaScript is parsing the page URL, that only the browser will know.

On the other hand the XSLT generates the HTML page - so it is executed BEFORE the browser even see the JavaScript to execute.

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.