如何使用xslt将XML转换为HTML

时间:2015-05-17 07:23:17

标签: html xml html5 xslt xslt-1.0

您好我想将我的xml数据转换为HTML报告。我的示例xml看起来有点像下面。



<?xml version="1.0" encoding="UTF-8"?>
<TestCases>
	<TestCase>
		<TCiD>TC001</TCiD>
		<Scenario>Verify the login functionality</Scenario>
		<TSResult>Pass</TSResult>
		<ScTime>50</ScTime>
		<Steps>
			<Step>Invoke application and enter a valid "Username"</Step>
			<Method>enter_Text</Method>
			<TestData>muralitharan_qac_mem</TestData>
			<StepResult>Pass</StepResult>
			<Time>08/05/15 14:34:27	</Time>
			<Filepath>SanpFilePath</Filepath>
		</Steps>
		<Steps>
			<Step>Enter the valid "Password"</Step>
			<Method>enter_Password</Method>
			<TestData>test!</TestData>
			<StepResult>Pass</StepResult>
			<Time>08/05/15 14:34:27	</Time>
			<Filepath></Filepath>
		</Steps>
		<Steps>
			<Step>Tap on the submit button</Step>
			<Method>click_On_Button</Method>
			<TestData></TestData>
			<StepResult>Pass</StepResult>
			<Time>08/05/15 14:34:27	</Time>
			<Filepath></Filepath>
		</Steps>    
		<Steps>
			<Step>Tool wait time</Step>
			<Method>ToolToWaitInMilliSeconds</Method>
			<TestData>10000</TestData>
			<StepResult>Pass</StepResult>
			<Time>08/05/15 14:34:27	</Time>
			<Filepath></Filepath>
		</Steps>    
	</TestCase>
	<TestCase>
		<TCiD>TC002</TCiD>
		<Scenario>Verify unsuccessful login for invalid Username</Scenario>
		<TSResult>Pass</TSResult>
		<ScTime>60</ScTime>
		<Steps>
			<Step>Invoke application and enter a invalid "Username"</Step>
			<Method>enter_Text</Method>
			<TestData>invalid_mem</TestData>
			<StepResult>Pass</StepResult>
			<Time>08/05/15 14:34:27	</Time>
			<Filepath>SanpFilePath</Filepath>
		</Steps>
		<Steps>
			<Step>Enter the valid "Password"</Step>
			<Method>enter_Password</Method>
			<TestData>test2!</TestData>
			<StepResult>Pass</StepResult>
			<Time>08/05/15 14:34:27	</Time>
			<Filepath></Filepath>
		</Steps>
		<Steps>
			<Step>Tap on the submit button</Step>
			<Method>click_On_Button</Method>
			<TestData></TestData>
			<StepResult>Pass</StepResult>
			<Time>08/05/15 14:34:27	</Time>
			<Filepath></Filepath>
		</Steps>     
	</TestCase>
</TestCases> 
&#13;
&#13;
&#13;

现在我希望我的html报告如何:

&#13;
&#13;
TestCase   Testcase Name                           Result    Time
TC001      Verify the login functionality          Pass      50
TC002      Verify unsuccessful login for           Pass      60
           invalid Username
&#13;
&#13;
&#13;

单击TC001时,测试用例的相关步骤将显示为一个表(类似于可以展开或折叠的树结构)。

任何人都可以帮助我实现这一目标。

我创建的xslt在表中显示了Testcases,但我无法为这些步骤创建树结构。 xslt如下。

<?xml version="1.0" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="http://www.stylusstudio.com/xquery">
    <xsl:template match="/">
        <html>
            <head /> 
            <body>
                <div style="text-align: none;color: #0000FF;">
                    <table align="none" style="border-style: outset;border-width: 2pt;width: 100%;background-repeat: repeat" cellspacing="1pt">
                        <tbody>
                            <tr>
                                <td style="border-style: inset;border-width: 2pt;padding: 2pt;vertical-align: middle;background-repeat: repeat">
                                    <div style="text-align: none;color: #0000FF;">
                                        <span style="font-weight: bold;text-decoration: underline;">
                                            <xsl:text>Test Case ID</xsl:text> 
                                        </span>
                                    </div>
                                </td>
                                <td style="border-style: inset;border-width: 2pt;padding: 2pt;vertical-align: middle;background-repeat: repeat">
                                    <div style="text-align: none;color: #0000FF;">
                                        <span style="font-weight: bold;text-decoration: underline;">
                                            <xsl:text>Test Case Name</xsl:text> 
                                        </span>
                                    </div>
                                </td>
                                <td style="border-style: inset;border-width: 2pt;padding: 2pt;vertical-align: middle;background-repeat: repeat">
                                    <div style="text-align: none;color: #0000FF;">
                                        <span style="font-weight: bold;text-decoration: underline;">
                                            <xsl:text>Result</xsl:text> 
                                        </span>
                                    </div>
                                </td>
                                <td style="border-style: inset;border-width: 2pt;padding: 2pt;vertical-align: middle;background-repeat: repeat">
                                    <div style="text-align: none;color: #0000FF;">
                                        <span style="font-weight: bold;text-decoration: underline;">
                                            <xsl:text>Execution Time (Seconds)</xsl:text> 
                                        </span>
                                    </div>
                                </td>
                            </tr>
                            <xsl:for-each select="/TestCases/TestCase">
                                <xsl:variable name="TestCase" select="." /> 
                                <tr>
                                    <td style="border-style: inset;border-width: 2pt;padding: 2pt;vertical-align: middle;background-repeat: repeat">
                                        <div style="text-align: none;color: #0000FF;">
                                            <xsl:value-of select="TCiD" /> 
                                        </div>
                                    </td>
                                    <td style="border-style: inset;border-width: 2pt;padding: 2pt;vertical-align: middle;background-repeat: repeat">
                                        <div style="text-align: none;color: #0000FF;">
                                            <xsl:value-of select="Scenario" /> 
                                        </div>
                                    </td>
                                    <td style="border-style: inset;border-width: 2pt;padding: 2pt;vertical-align: middle;background-repeat: repeat">
                                        <div style="text-align: none;color: #0000FF;">
                                            <xsl:value-of select="TSResult" /> 
                                        </div>
                                    </td>
                                    <td style="border-style: inset;border-width: 2pt;padding: 2pt;vertical-align: middle;background-repeat: repeat">
                                        <div style="text-align: none;color: #0000FF;">
                                            <xsl:value-of select="ScTime" /> 
                                        </div>
                                    </td>
                                </tr>
                            </xsl:for-each>
                        </tbody>
                    </table>
                </div>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

我认为这site可以帮助你。

相关问题