gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:59:44 25 4
gpt4 key购买 nike

这已经被问过,但我想评估这是否可能?...

是否有一种简单的方法将 javascript 变量传递到 xsl 变量?原因是,变量将来自外部脚本。

这是我的非工作代码...

<?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>

我想在网页上显示“Term1”。

有什么想法吗?

最佳答案

script 标记由 XSL 输出,它并不是定义在 XSL 上下文中可执行的任何内容。您可以做的是在全局范围内定义 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>

如果你想在 JS 执行时有一个 JS 知道的变量,并用它做一些事情,比如将它显示在浏览器页面的元素中,那么你必须在 JS 中这样做:

<xsl:template match="/">

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

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

</xsl:template>

关于javascript - 如何将javascript变量传递给xsl变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25771562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com