Xslt没有使用变压器工厂读取xml

时间:2018-02-07 01:28:38

标签: xml xslt

有人可以帮助我,我做错了什么。 我正在拧一个函数来将OATS测试执行xml转换为格式化的HTML。

我使用下面的程序将xml转换为带有xslt的html。

    Source xml = new StreamSource(new File("C:\\Reports\\ERseouce.xml"));
    Source xslt = new StreamSource("C:\\Reports\\ExecutionReport_source.xsl");
    StringWriter sw = new StringWriter();
    FileWriter fw = new FileWriter("C:\\Reports\\ExecutionReport_source.html");
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer trasform = tFactory.newTransformer(xslt);
    trasform.transform(xml, new StreamResult(sw));
    fw.write(sw.toString());
    fw.close();

请在下面找到xml:

<?xml version="1.0" encoding="utf-8"?>
<ROOT action_count="12" action_fail_count="0" action_pass_count="12" action_warning_count="0" anyPlaybackTimes="true" anyRecordedTimes="false" anyResponseTimes="true" anyTimeStamp="true" comment_fail_count="0" comment_pass_count="0" comment_warning_count="0" fail_count="0" iteration_num="1" openscript_version="13.2.0.1.173" page_count="8" pass_count="0" playback_time="132700" response_time="0" result="[1]" result_flag="[1]" script="C:\OracleATS\OFT\SupplierInquiry" script_name="SupplierInquiry" test_count="0" test_date="2/02/2018 19:28:43 PM EST (UTC -5:00)" time_stamp="02-02 19:28:42" total_fail_count="0" total_pass_count="12" total_test_count="12" total_warning_count="0" warning_count="0">
</ROOT>

请在下面找到xslt。

     <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">       
        <xsl:template match="/">
            <html>       
                <head><img   style="float: left;" src="logo.gif" alt="logo" />
                            <h1 style="font-size:60px;text-align: center;">Test Execution Report</h1>

                </head>
                        <body>
                    <table>
                                                <tr>
                            <td><xsl:value-of select = "script_name"/> </td>
                        </tr>
                        <tr>
                            <td><xsl:value-of select = "test_date" /></td>
                        </tr>
                        <tr>
                            <td><xsl:value-of select = "total_pass_count"/> </td>
                        </tr>
                        <tr>
                            <td><xsl:value-of select = "total_fail_count" /> </td>
                        </tr>
                                            </table>
                        </body>
            </html>
        </xsl:template>
    </xsl:stylesheet>

我正在获得以下html输出

    <html>
    <head>
    <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <img style="float: left;" src="logo.gif" alt="logo"><h1 style="font-size:60px;text-align: center;">Test Execution Report</h1>
    </head>
    <body>
    <table>
    <tr>
    <td></td>
    </tr>
    <tr>
    <td></td>
    </tr>
    <tr>
    <td></td>
    </tr>
    <tr>
    <td></td>
    </tr>
    </table>
    </body>
    </html> 

1 个答案:

答案 0 :(得分:0)

为了访问XSLT中的属性值,在属性名称之前使用@。例如。 @script_name

请修改XSLT模板,如下所示。

<xsl:template match="ROOT">
    <html>
        <head>
            <img style="float: left;" src="logo.gif" alt="logo" />
            <h1 style="font-size:60px;text-align: center;">Test Execution Report</h1>
        </head>
        <body>
            <table>
                <tr>
                    <td><xsl:value-of select="@script_name" /></td>
                </tr>
                <tr>
                    <td><xsl:value-of select="@test_date" /></td>
                </tr>
                <tr>
                    <td><xsl:value-of select="@total_pass_count" /></td>
                </tr>
                <tr>
                    <td><xsl:value-of select="@total_fail_count" /></td>
                </tr>
            </table>
        </body>
    </html>
</xsl:template>

以下是输出

<html>
    <head>
        <img style="float: left;" src="logo.gif" alt="logo" />
        <h1 style="font-size:60px;text-align: center;">Test Execution Report</h1>
    </head>
    <body>
        <table>
            <tr>
                <td>SupplierInquiry</td>
            </tr>
            <tr>
                <td>2/02/2018 19:28:43 PM EST (UTC -5:00)</td>
            </tr>
            <tr>
                <td>12</td>
            </tr>
            <tr>
                <td>0</td>
            </tr>
        </table>
    </body>
</html>