例如,我有一个xml文档:
<?xml version="1.0" encoding="UTF-8"?>
<tests>
<testrun run="test1">
<test name="foo" pass="true" />
<test name="bar" pass="true" />
<test name="baz" pass="true" />
</testrun>
<testrun run="test2">
<test name="foo" pass="true" />
<test name="bar" pass="false" />
<test name="baz" pass="false" />
</testrun>
</tests>
如果我将它转换为带有xsl文档的html,我可能会得到如下输出:
<html>
<body>test1<table>
<tr>
<td>Test</td><td>Pass</td>
</tr>
<tr>
<td>foo</td><td>true</td>
</tr>
<tr>
<td>bar</td><td>true</td>
</tr>
<tr>
<td>baz</td><td>true</td>
</tr>
</table>test2<table>
<tr>
<td>Test</td><td>Pass</td>
</tr>
<tr>
<td>foo</td><td>true</td>
</tr>
<tr>
<td>bar</td><td>false</td>
</tr>
<tr>
<td>baz</td><td>false</td>
</tr>
</table>
</body>
我的问题如下:
我如何得到这个输出:
<html>
<body>test1<table>
<tr>
<td>Test</td><td>Pass</td>
</tr>
<tr>
<td>foo</td><td>true</td>
</tr>
<tr>
<td>bar</td><td>true</td>
</tr>
<tr>
<td>baz</td><td>true</td>
</tr>
</table>
</body>
和此:
<html>
<body>test2<table>
<tr>
<td>Test</td><td>Pass</td
</tr>
<tr>
<td>foo</td><td>true</td>
</tr>
<tr>
<td>bar</td><td>false</td>
</tr>
<tr>
<td>baz</td><td>false</td>
</tr>
</table>
</body>
</html>
我怎么能得到它,有没有人有意见?
答案 0 :(得分:0)
如果我理解你的问题,以下就可以了。我不确定,你的意思是html字符串。如果您需要输出实际的单个html文档,这还不够:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<!-- This template is applied for each 'testrun' node in the xml document. It outputs the html, body and table tags and processes all children of the 'testrun' nodes according to any other templates.-->
<xsl:template match="testrun">
<hmtl>
<body>
<xsl:value-of select="@run"/>
<table>
<xsl:apply-templates/>
</table>
</body>
</hmtl>
</xsl:template>
<!-- This template prevents that the 'tests' node is output, and makes sure that all child nodes of 'tests' nodes have matching templates applied to them. -->
<xsl:template match="tests">
<xsl:apply-templates></xsl:apply-templates>
</xsl:template>
<!-- This template copies every single node from the xml without doing anything
other than making an exact copy. The other templates override this template.-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--This template makes sure that a table row with two cells with the values of the 'name' and 'pass' attributes of 'test' nodes are output for each 'test' node.-->
<xsl:template match="test">
<tr>
<td><xsl:value-of select="@name"/></td>
<td><xsl:value-of select="@pass"/></td>
</tr>
</xsl:template>
</xsl:transform>
编辑:根据要求添加了模板说明。