如何将XSL变量传递给Javascript函数?

时间:2013-04-05 08:16:45

标签: javascript xslt

以下是我的示例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="/">
        <html>
            <head>
                <script type="text/javascript">
                //<![CDATA[
                function showHide(elementid){
                    if (document.getElementById(elementid).style.display == 'none'){
                        document.getElementById(elementid).style.display = '';
                    } else {
                        document.getElementById(elementid).style.display = 'none';
                    }
                }
                //]]></script>
                <!-- Inserting pie chart  -->
                <script type="text/javascript" src="https://www.google.com/jsapi"></script>
                <script type="text/javascript">
                //<![CDATA[
                google.load("visualization", "1", {packages:["corechart"]});
                google.setOnLoadCallback(drawChart);
                function drawChart() {
                    var data = google.visualization.arrayToDataTable([
                        ['Task', 'Hours per Day'],
                        ['Work',     11],
                        ['Eat',      2],
                        ['Commute',  2],
                        ['Watch TV', 2],
                        ['Sleep',    7]
                    ]);

                    var options = {
                        title: 'My Daily Activities'
                    };

                    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
                    chart.draw(data, options);
                }
            //]]></script>
            <!--Finishing pie chRt -->
            </head>
            <body>
                <h2>eBuilder Automation Test Suite-Summary</h2>
                <div id="chart_div" style="width: 600px; height: 400px;"></div>
                <table border="1">
                    <tr bgcolor="#808080">
                        <th>Name</th>
                        <th>Total TCS</th>
                        <th>Passed</th>
                        <th>Skipped</th>
                        <th>Errors</th>
                        <th>Failures</th>
                        <th>Pass Rate</th>
                        <th>Tot Time Taken</th>
                    </tr>
                    <xsl:for-each select="testsuite">
                        <xsl:variable name="PassCount">
                            <number>
                                <xsl:value-of select="(@tests - @skipped - @errors - @failures)"/>
                            </number>
                        </xsl:variable>
                        <tr>
                            <td>
                                <xsl:value-of select="@name"/>
                            </td>
                            <td>
                                <xsl:value-of select="@tests"/>
                            </td>

                            <td bgcolor="#32CD32">
                                <xsl:value-of select="(@tests - @skipped - @errors - @failures)"/>
                            </td>
                            <td bgcolor="#FFFF00">
                                <xsl:value-of select="@skipped"/>
                            </td>
                            <td>
                                <xsl:value-of select="@errors"/>
                            </td>
                            <td bgcolor="#FF0000">
                                <xsl:value-of select="@failures"/>
                            </td>
                            <td>
                                <font color="red">
                                    <xsl:value-of select="(msxsl:node-set($PassCount)/number) div @tests * 100"/>%
                                </font> 
                            </td>
                            <td bgcolor="#008000">
                                <xsl:value-of select="@time"/>
                            </td>
                            <!--xsl:value-of select="@failures div @tests *100"/-->
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

我想像下面那样传递XSL变量PassCount

var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Work',     passcount],
          ['Eat',      2],
          ['Commute',  2],
          ['Watch TV', 2],
          ['Sleep',    7]
        ]);

有没有聪明的方法可以做到这一点?

1 个答案:

答案 0 :(得分:1)

希望我回答你的问题(我不确定,但无论如何)

如果您不使用CDATA,使用xslt生成javascript应该没有问题。 您可以使用xsl:value-of将xslt var值放在您喜欢的任何位置。 但是如果没有CDATA,你必须在javascript代码中转义一些字符(&lt;,&amp;)。

 'Work',     <xsl:value-of select="(msxsl:node-set($PassCount)/number)/> ],

var passcount = <xsl:value-of select="(msxsl:node-set($PassCount)/number)/>;
....
['Work',     passcount],