【发布时间】:2020-12-12 17:56:51
【问题描述】:
我在 XML 的 XSLT 转换中遇到了 For-Each 的一些问题。 XML 包含多个子节点:
<?xml version="1.0" encoding="utf-8"?>
<testsuites duration="6376 ms">
<testsuite>
<testcase>
<testid>A1</testid>
<package>Package 1</package>
<test>Test 1</test>
<duration>2 ms</duration>
<failures>0</failures>
<pass>4</pass>
<testparts>
<testpart>
<time>2020-08-23-17-03-24</time>
<status>Test passed</status>
<test>Assertion 1</test>
</testpart>
<testpart>
<time>2020-08-23-17-03-24</time>
<status>Test passed</status>
<test>Assertion 2</test>
</testpart>
<testpart>
<time>2020-08-23-17-03-24</time>
<status>Test passed</status>
<test>Assertion 3</test>
</testpart>
</testparts>
</testcase>
</testsuite>
.......
XSLT 文件是:
<?xml version="1.0" encoding="utf-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8" />
...Some styles and scripts
</head>
<body>
<div>
<xsl:for-each select="testsuites/testsuite/testcase">
<span class="column1"><xsl:value-of select="duration"/></span>
<span class="PackageStatus"><xsl:value-of select="package"/></span>
<span class="Function"><xsl:value-of select="test"/></span>
<span class="Message" name="ID0AFH0IHId"><xsl:value-of select="failures"/></span>
<span class="Message" name="ID0AFH0IHId"><xsl:value-of select="pass"/></span>
<span class="Message" name="ID0AFH0IHId"><xsl:value-of select="pass"/>Show Assertions</span>
<div>
<xsl:for-each select="testsuites/testsuite/testcase/testparts/testpart">
<span class="column1"><xsl:value-of select="time"/></span>
<span class="passed"><xsl:value-of select="status"/></span>
<span class="Function"><xsl:value-of select="test"/></span>
</xsl:for-each>
</div>
</xsl:for-each>
</div>
</body>
</html>
您可以看到有多个测试用例,并且在其下方,testparts 有多个 testpart 元素。 现在第一个 foreach 正在按预期工作,但内部的没有工作
【问题讨论】:
-
我认为那是因为你有内部 for-each 的选择的绝对路径,尝试改为仅 select="testparts/testpart"
-
Bingo..thanks @nordenvall,现在解决了,非常感谢