1

下面是我的示例 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]
        ]);

有什么聪明的方法可以做到这一点吗?

4

1 回答 1

1

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

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

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

或者

var passcount = <xsl:value-of select="(msxsl:node-set($PassCount)/number)/>;
....
['Work',     passcount],
于 2013-04-10T15:11:53.313 回答