简体   繁体   English

如何将javascript变量传递给xsl变量?

[英]How to pass javascript variable to xsl variable?

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? 有没有一种简单的方法可以将javascript变量传递给xsl变量? 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. 我想在网页上显示“ Term1”。

Any idea? 任何想法?

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. XSL正在输出script标记,它不是定义在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变量,然后在脚本和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: 如果要在执行JS时让JS知道一个变量,并对其进行某种处理,例如将其显示在浏览器页面的元素中,则必须在JS中执行此操作:

<xsl:template match="/">

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

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

</xsl:template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM