1

This has been asked before, but I want to evaluate if this is possible?...

Is there a simple way to pass javascript variable into an xsl variable? Reason being, the variable will be coming from an external script.

Here's my non-working code...

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">

    <xsl:template match="/">

    <div>
    <script type="text/javascript">
    var key = window.location.href;
    </script>

    <xsl:variable name="jsvar">$key</xsl:variable>
    <xsl:value-of select="$jsvar"/>

    </div>
      </xsl:template>

I want to display "Term1" on the web page.

Any idea?

5
  • how are you running the XSL transform? Commented Sep 10, 2014 at 17:40
  • @dandavis - not on my code above... do you have an idea on the approach for this use-case of mine? using transformation? Commented Sep 10, 2014 at 17:45
  • it depends on how you run the XSLT, shich we can't see. please post code if you want help with code. Commented Sep 10, 2014 at 17:46
  • I've updated my code, but that's the only items I'm starting with. Commented Sep 10, 2014 at 17:51
  • you can't set variables using the XSL as a tack-on stylesheet to an xml file, you need to use an XSL engine, and the manner in which vars are set depends on the tool. post that code and we can advise. Commented Sep 10, 2014 at 17:52

1 Answer 1

1

The script tag is being output by the XSL, it's not something that defines anything that's executable in the context of the XSL. What you can do is define the XSL variable at global scope and re-use it both in the script and the div:

<xsl:variable name="jsvar">Term1</xsl:variable>

<xsl:template match="/">

    <script>
        var key = "<xsl:value-of select="$jsvar"/>";
    </script>

    <div>
        <xsl:value-of select="$jsvar"/>
    </div>

</xsl:template>

If you want a variable known to JS when JS is executing and do something with it like display it in an element in the browser page, then you have to do that in the JS:

<xsl:template match="/">

    <script>
        window.onload = function() {
            document.getElementById('location').textContent = window.location.href;
        };
    </script>

    <div id="location"></div>

</xsl:template>
Sign up to request clarification or add additional context in comments.

5 Comments

This will work, but my use-case scenario is that "Term1" will vary and will be supplied from an external javascript. It can be "Term2" or "AnotherTerm1"... My starting point is from a javascript variable into xsl "value-of"...
What do you mean by "supplied from an external javascript"? Where does that javascript live and how is it supplying anything to XSL? Is the JS invoking XSL somehow, in which case you could use the XSL param mechanism?
Yes, the javascript is from an external. Any simple approach on how to use the param with my use-case scenario? Sorry I'm not too familiar using <param>...
XSLT cannot parse, nor execute, external JS. As @dandavis asked in his comment, how are you running the XSL? From the command line? In that case you could pass the value you are concerned about as an XML parameter, the command line option for doing which will depend on your XSL engine; it might be something like '-Djsvar=Term1', for example. But actually you really need to tell us the entire flow of what you are trying to do, because it seems like you're mixed up about the relationship between JavaScript and XSL.
I made some specific use case like window.location as my variable to pass on to the xsl variable. I've edited my code above. Will this make sense?

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.