[英]Set XSL variable with a script generated value
I am trying to get a value generated by a variable in XSL
我正在尝试获取由XSL
中的变量生成的值
<xsl:variable name="Value">
<script type="text/javascript">
var DistX = <xsl:value-of select="DistX"/>;
var DistY = <xsl:value-of select="DistY"/>;
var PadSize = <xsl:value-of select="PadSize"/>;
var Value = Math.trunc(Math.sqrt(((DistX/PadSize*100) * (DistX/PadSize*100)) + ((DistY/PadSize*100) * (DistY/PadSize*100)) ),0.1);
document.write(Value);
console.log(Value);
</script>
</xsl:variable>
<xsl:value-of select="$Value"></xsl:value-of>
The result of this display in table <td>
:在表<td>
中显示的结果:
var DistX = 0.9;
var DistY = 0.3;
var PadSize = 2.6;
var Value = Math.trunc(Math.sqrt(((DistX/PadSize*100) * (DistX/PadSize*100)) + ((DistY/PadSize*100) * (DistY/PadSize*100)) ),0.1);
document.write(Value);
console.log(Value);
and not calculated value 54% I want make an if
statement on this value to change the font color.而不是计算值 54% 我想在这个值上做一个if
语句来改变字体颜色。
Usually XSLT is executed before and independent of any JavaScript execution thus any sequence which might make sense is to simply create script element doing the computation and outputting its computed value:通常 XSLT 在任何 JavaScript 执行之前执行且独立于任何执行,因此任何可能有意义的序列都是简单地创建脚本元素进行计算并输出其计算值:
<script type="text/javascript">
var DistX = <xsl:value-of select="DistX"/>;
var DistY = <xsl:value-of select="DistY"/>;
var PadSize = <xsl:value-of select="PadSize"/>;
var Value = Math.trunc(Math.sqrt(((DistX/PadSize*100) * (DistX/PadSize*100)) + ((DistY/PadSize*100) * (DistY/PadSize*100)) ),0.1);
document.write(Value);
</script>
without nesting it into an xsl:variable
.无需将其嵌套到xsl:variable
中。
Note that document.write
is not supported in client-side script in Mozilla browsers if the XSLT is also run client-side (eg with <?xml-stylesheet?>
pi).请注意,如果 XSLT 也在客户端运行(例如,使用<?xml-stylesheet?>
pi),则 Mozilla 浏览器的客户端脚本不支持document.write
。
Assuming you're running XSLT in the browser, you could upgrade to XSLT 3.0 by using Saxon-JS, and then do it all in XSLT (no Javascript needed) by writing Assuming you're running XSLT in the browser, you could upgrade to XSLT 3.0 by using Saxon-JS, and then do it all in XSLT (no Javascript needed) by writing
<xsl:variable name="Value" select="round(math:sqrt(((DistX/PadSize*100) *
(DistX/PadSize*100)) + ((DistY/PadSize*100) * (DistY/PadSize*100))))"/>
(You seem to be calling Math.trunc() with two arguments, I'm not sure what that does exactly, but there will be an XPath equivalent). (您似乎在用两个 arguments 调用 Math.trunc(),我不确定这到底是做什么的,但会有一个 XPath 等效项)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.