我正在使用xsl将我的xml文件转换为包含表格的html文件。表格一旦形成,我就会调用一个外部javascript函数来计算发生的失败次数。现在根据失败次数,我必须将html文件作为附件发送给电子邮件。我一直在寻找关于如何在xsl中访问外部javascript变量的不同方法。谷歌搜索时我甚至尝试了一些方法,但徒劳无功!我想" noOfOccurance"要传递给xsl文件的变量值。任何帮助深表感谢!谢谢!
这是我的xsl文件:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
>
<xsl:template match="/">
<html>
<head><title>Olahtek Automation</title></head>
<body>
<table border="1">
<tr>
<th style="color:Blue">TestCase</th>
<th style="color:Blue">Number Of Tests</th>
<th style="color:Blue">Failures</th>
<th style="color:Blue">Result</th>
</tr>
<xsl:for-each select="testsuites/testsuite">
<tr>
<td><xsl:value-of select="@name"/></td>
<td><xsl:value-of select="@tests"/></td>
<td><xsl:value-of select="@failures"/></td>
<xsl:choose>
<xsl:when test="@failures > 0">
<td style="color:red">Fail</td>
</xsl:when>
<xsl:otherwise>
<td style="color:green">Pass</td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript"
src="/usr/lib/node_modules/protractor/ola_auto/count.js">
</script>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
这是我的外部javascript文件 - count.js
getOccurance("Fail");
function getOccurance(word)
{
$("table").each(function (tindx, tobj)
{
var noOfOccurance = 0;
$("tr td:gt(1)", $(tobj)).each(function (ind, obj)
{
if (word == $.trim($(obj).text())) noOfOccurance++;
});
document.write("Number Of Failures : " +noOfOccurance);
var cu = document.URL;
var lastPart = cu.split("/").pop();
document.write("<br>");
document.write("Test Case Name : " +lastPart);
})
}
答案 0 :(得分:1)
另一个问题,既然我知道失败的数量,我是否可以从xslt发送电子邮件?
最好避免在StackOverflow上提出补充问题。打开一个新主题以提出新问题。
我认为没有办法从浏览器中运行的代码发送电子邮件,无论是XSLT代码还是Javascript代码:您需要通过服务器上运行的东西来实现。 (见How to send an email from JavaScript)
我的同事在XML London 2017上发表了一篇论文,展示了一个应用程序,其中XSLT代码在客户端和服务器上运行,通过HTTP进行通信,部分应用程序涉及使用Saxon的saxon发送电子邮件:sendmail XSLT扩展功能(运行于服务器:客户端运行Saxon-JS。
答案 1 :(得分:0)
这是不可能的。在xslt完成处理之后很久,浏览器就会运行JavaScript。但是通过xsl找到你想要的东西是微不足道的。
<xsl:value-of select="count(testsuites/testsuite[@failures > 0])" />