简体   繁体   English

使用脚本生成的值设置 XSL 变量

[英]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语句来改变字体颜色。

bad display显示不良

good display made by paint power;)油漆动力带来的良好展示;)

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.

相关问题 如何将 xsl 变量值传递给 javascript function - how to pass a xsl variable value to a javascript function 当值是 Camunda 中的数组时如何在脚本任务中设置变量 - How to set variable in Script Task when Value is an Array in Camunda 传递值 <xsl:value-of> 到Java脚本功能 - Passing a value of <xsl:value-of> to a java script function 如何在脚本标签内将 xsl 变量作为参数传递给 javascript function? - How to pass xsl variable as a parameter to javascript function within script tag? 如何访问外部Java脚本变量并在xsl中打印它? - How to access an external java script variable and print it in xsl? 动态生成的下拉列表的设置值 - Set Value of a Dynamically Generated DropDown 将php变量设置为等于从“ while($ row = mysql_fetch_array”生成的表单元格的值 - Set php variable equal to value of table cells generated from “while($row = mysql_fetch_array” 在xsl select语句中,如何测试js变量的值 - In an xsl choose statement how can I test for the value of a js variable 如何从aspx文件中的脚本中将值设置为脚本(.js)文件中的变量 - How to set the value to a variable in the script(.js) file from the script in aspx file 我可以在脚本中设置一个等于按钮值的变量,而不是更改变量以更改此按钮的名称 - Can I set a variable in script equal to a value of button, than i change the variable to change this button's name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM