当前位置: 首页 > 知识库问答 >
问题:

如何使用xslt将值从XML追加到TestNG可电子邮件HTML报表

熊朝
2023-03-14

我只是有一些动态XML文件,其值如下所示:

 <testCaseId>CSPDDataObject [testCaseId=US_KINDLE_22, description=RISK with AUTH-fulfillment phase]</testCaseId>
 <message>Some logger details</message>
 <exception>java.lang.AssertionError: expected [true] but found [false]</exception>

完整的XML结构:

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <FailedDetails>
    <FailedMessage>
        <testCaseId>US_KINDLE_TC1</testCaseId>
        <message> some logger</message>
        <exception>java.lang.AssertionError: expected [true] but found [false]
        </exception>
     </FailedMessage>
     <FailedMessage>
        <testCaseId>US_KINDLE_TC2</testCaseId>
        <message> some logger</message>
        <exception>java.lang.AssertionError: expected [true] but found [false]
        </exception>
    </FailedMessage>
       ......(n) (It repeats 'n' times)
  </FailedDetails>

我只想在下面的标记中将这些值追加到现有的testNG可电子邮件报告中:

 <h3> </h3><table> <tr> <td> need to append the above one with respective tag value</td></tr></table>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>TestNG Report</title><style type="text/css">table {margin-bottom:10px;border-collapse:collapse;empty-cells:show}th,td {border:1px solid #009;padding:.25em .5em}th {vertical-align:bottom}td {vertical-align:top}table a {font-weight:bold}.stripe td {background-color: #E6EBF9}.num {text-align:right}.passedodd td {background-color: #3F3}.passedeven td {background-color: #0A0}.skippedodd td {background-color: #DDD}.skippedeven td {background-color: #CCC}.failedodd td,.attn {background-color: #F33}.failedeven td,.stripe .attn {background-color: #D00}.stacktrace {white-space:pre;font-family:monospace}.totop {font-size:85%;text-align:center;border-bottom:2px solid #000}</style></head><body><table><tr><th>Test</th><th># Passed</th><th># Skipped</th><th># Failed</th><th>Time (ms)</th><th>Included Groups</th><th>Excluded Groups</th></tr><tr><th colspan="7">TransferMoney-Suite</th></tr><tr><td><a href="#t0">KindleReaderTest</a></td><td class="num">41</td><td class="num">0</td><td class="num attn">17</td><td class="num">10,316,154</td><td></td><td></td></tr><tr class="stripe"><td><a href="#t1">Suite setup and teardown</a></td><td class="num">0</td><td class="num">0</td><td class="num">0</td><td class="num">2,521</td><td></td><td></td></tr><tr><th>Total</th><th class="num">41</th><th class="num">0</th><th class="num attn">17</th><th class="num">10,318,675</th><th colspan="2"></th></tr></table><table><thead><tr><th>Class</th><th>Method</th><th>Start</th><th>Time (ms)</th></tr></thead><tbody><tr><th colspan="4">TransferMoney-Suite</th></tr></tbody><tbody id="t0"><tr><th colspan="4">KindleReaderTest &#8212; failed</th></tr><tr class="failedeven"><td rowspan="17">com.amazon.e2e.device.os22</td><td><a href="#m0">KindleReaderTest</a></td><td rowspan="17">1540236982369</td><td rowspan="17">116625</td></tr><tr class="failedeven"><td><a href="#m1">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m2">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m3">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m4">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m5">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m6">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m7">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m8">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m9">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m10">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m11">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m12">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m13">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m14">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m15">KindleReaderTest</a></td></tr><tr class="failedeven"><td><a href="#m16">KindleReaderTest</a></td></tr><tr><th colspan="4">KindleReaderTest &#8212; passed</th></tr><tr class="passedeven"><td rowspan="41">com.amazon.e2e.device.os22</td><td><a href="#m17">KindleReaderTest</a></td><td rowspan="41">1540241421467</td><td rowspan="41">218092</td></tr><tr class="passedeven"><td><a href="#m18">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m19">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m20">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m21">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m22">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m23">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m24">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m25">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m26">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m27">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m28">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m29">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m30">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m31">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m32">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m33">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m34">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m35">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m36">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m37">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m38">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m39">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m40">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m41">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m42">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m43">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m44">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m45">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m46">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m47">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m48">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m49">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m50">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m51">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m52">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m53">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m54">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m55">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m56">KindleReaderTest</a></td></tr><tr class="passedeven"><td><a href="#m57">KindleReaderTest</a></td></tr></tbody><tbody id="t1"></tbody></table><h2>KindleReaderTest</h2><h3 id="m0">com.amazon.e2e.device.os22#KindleReaderTest</h3><table class="result"><tr class="param"><th>Parameter #1</th></tr><tr class="param stripe"><td>CSPDDataObject [testCaseId=US_KINDLE_22, description=RISK with AUTH-fulfillment phase]</td></tr><tr><th>Messages</th></tr><tr><td><div class="messages">{
  SOME LOGGER MESSAGE GOES HERE
}</div></td></tr><tr><th>Exception</th></tr><tr><td><div class="stacktrace">java.lang.AssertionError: expected [true] but found [false]
</div></td></tr></table><p class="totop"><a href="#summary">back to summary</a></p>
<h2>Suite setup and teardown</h2></body></html>

共有1个答案

鲜于俊侠
2023-03-14

这个xslt将读取html文件,并在表[@class='result']后面追加xml节点。当parinsg html时,doctype将导致错误。因此,在运行xlst之前,删除HTML中的第一行。

XLS文件

<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/1999/xhtml"
                xpath-default-namespace="http://www.w3.org/1999/xhtml">
    <xsl:output method="html" indent="yes" omit-xml-declaration="yes"
                doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
                doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" />
    <xsl:param name="failedxml.outputDir" />
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:variable name="doc" select="document($failedxml.outputDir)" />
    <xsl:template match="body/table[@class='result']">
        <xsl:copy-of select="."/>
        <xsl:for-each select="$doc/node()/child::element()">
            <table class="result">
                <tr class="param">
                    <th>Parameter #1</th>
                </tr>
                <tr class="param stripe">
                    <td> <xsl:value-of select ="child::element()[1]"/></td>
                </tr>
                <tr>
                    <th>Messages</th>
                </tr>
                <tr>
                    <td>
                        <div class="messages"><xsl:value-of select ="child::element()[2]"/></div>
                    </td>
                </tr>
                <tr>
                    <th>Exception</th>
                </tr>
                <tr>
                    <td>
                        <div class="stacktrace"><xsl:value-of select ="child::element()[3]"/></div>
                    </td>
                </tr>
            </table>

        </xsl:for-each>

    </xsl:template>
</xsl:stylesheet>

我用gradle跑,

configurations{ xslt }
dependencies {
    xslt    'net.sf.saxon:saxon:8.7'
}
task generateReport << {
    File reportDir=new File("${projectDir}/HTML_Reports")
    if(reportDir.exists()){
        reportDir.deleteDir()
    }
    reportDir.mkdir()

   File emailreport=new File("${projectDir}/emailable-report.html")
   // Write some script to remove DOCTYPE from html file before running xslt.

    ant.xslt(in: "${testReportDir.absolutePath}/emailable-report.html",
             style: "${projectDir.absolutePath}/merge_html.xsl",
             out: "${reportDir.absolutePath}/index.html",
             classpath: configurations.xslt.asPath) {
            param(name: 'failedxml.outputDir', expression:  "${projectDir.absolutePath}/failedtest.xml")
        }
 类似资料: