0

I am using xsl to convert my xml file into a html file which includes a table. Once the table has been formed, Im calling an external javascript function which counts the number of failures occurred. Now based on the number of failures, I have to send the html file as an attachment with the email. I have been searching for different methods as to how to access the external javascript variable in xsl. I even tried few methods suggested when googling it out but in vain! I want "noOfOccurance" variable value to be passed to xsl file. Any help is much appreciated! Thank you!

Here is my xsl file :

<?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>

Here is my external javascript file - 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);
    }) 

}

2 Answers2

1

Just another question, Now that I know the number of failures, Is it possible for me to send an email from xslt?

It's best to avoid supplementary questions on StackOverflow. Open a new thread to ask a new question.

I don't think there's any way of sending an email from code running in the browser, whether it's XSLT code or Javascript code: you need to do that via something running on the server. (See How to send an email from JavaScript)

My colleagues published a paper at XML London 2017 showing an application with XSLT code running both on the client and the server, communicating via HTTP, and part of that application involved sending emails using Saxon's saxon:sendmail XSLT extension function (running on the server: the client side runs Saxon-JS).

http://xmllondon.com/2017/xmllondon-2017-proceedings.pdf

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

It is impossible. The JavaScript is run by the browser long after your xslt has finished processing. But what you want is trivial to find via xsl.

<xsl:value-of select="count(testsuites/testsuite[@failures &gt; 0])" />
Björn Tantau
  • 1,564
  • 14
  • 13
  • Thank you! This line does print the number of failures! I was aware that my script was run long after the xslt, but yea, wanted to know if there is a way I can access! – Sowmya MSK Jul 20 '17 at 12:37
  • Just another question, Now that I know the number of failures, Is it possible for me to send an email from xslt? – Sowmya MSK Jul 20 '17 at 12:53
  • Unless you have some extension capable of sending mails it is not possible to send mails via xslt. – Björn Tantau Jul 20 '17 at 14:05