我有一个xml文件,其中一个节点为:
<test-case time="0.077" name="TestingNunit.NUnitTestClass.NunitTestMethod1" asserts="1" success="False" result="Failure" executed="True">
<failure>
<message>
<![CDATA[ Expected: not 3.0d But was: 3.0d ]]>
</message>
<stack-trace>
<![CDATA[at TestingNunit.NUnitTestClass.NunitTestMethod1() in z:UnitTestingSampleUIAutomationTestingNunitUnitTest1.cs:line 12 ]]>
</stack-trace>
</failure>
</test-case>
<test-case time="0.003" name="TestingNunit.NUnitTestClass.NunitTestMethod2" asserts="2" success="False" result="Failure" executed="True">
<failure>
<message>
<![CDATA[ Expected: 2.0d But was: 3.0d ]]>
</message>
<stack-trace>
<![CDATA[at TestingNunit.NUnitTestClass.NunitTestMethod2() in z:UnitTestingSampleUIAutomationTestingNunitUnitTest1.cs:line 21 ]]>
</stack-trace>
</failure>
现在我想读取所有CDATA的数据"。我正在尝试使用XSL生成一个html文件,我需要这些数据?我可以为普通节点及其属性生成数据,但对于以这种方式编写的节点,我无法找到读取它的方法。这是我的XSL文件foreach循环
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>Test Method</th>
<th>Execution Time</th>
<th>Asserts Performed</th>
<th>Success</th>
<th>Result</th>
<th>Executed</th>
<th>Failure Message</th>
<th>Stack Trace</th>
</tr>
<xsl:for-each select="test-results/test-suite/results/test-suite/results/test-suite/results/test-case">
<tr>
<td><xsl:value-of select="@name"/></td>
<td><xsl:value-of select="@time"/></td>
<td><xsl:value-of select="@asserts"/></td>
<td><xsl:value-of select="@success"/></td>
<td><xsl:value-of select="@result"/></td>
<td><xsl:value-of select="@executed"/></td>
<td><xsl:value-of select="//failure/message/text()" disable-output-escaping="no"/></td>
<td><xsl:value-of select="//failure/stack-trace/text()" disable-output-escaping="no"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
text()
函数。
//test-case/failure/message/text()
//test-case/failure/stack-trace/text()
尝试select="./failure/message/text()"
而不是select="//failure/message/text()"
(请注意开头的./
)。
为此,XPath与for-each
循环当前选择的节点相关。当使用//
时,它将从文档中选择所有节点,忽略当前选择的节点。想象一下,这就像Linux上的ls
命令:ls /*
将选择顶级元素,而不考虑您当前的工作目录,而ls ./*
将选择您当前工作目录的子元素。