XSLT 1.0 - 用 HTML 包裹的多个子节点模板
XSLT 1.0 - Multiple Child Nodes template wrapped with HTML
我在使用来自 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 按预期工作,但内部的不工作
这更像是一个一般性的 XML 问题,但为了获得比我上面的评论更完整的答案,我参考 XSLT documentation regarding focus 说:
[Definition: The context item is the item currently being processed.
An item (see [XDM 3.0]) is either an atomic value (such as an integer,
date, or string), a node, or a function item. It changes whenever
instructions such as xsl:apply-templates and xsl:for-each are used to
process a sequence of items; each item in such a sequence becomes the
context item while that item is being processed.] The context item is
returned by the XPath expression . (dot).
因此,当您输入第一个 for-each 时,上下文已更改,您需要应用与该上下文相关的进一步选择,对于您的示例,从绝对路径更改为相对路径就足够了内循环的选择:
<xsl:for-each select="testparts/testpart">
我在使用来自 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 按预期工作,但内部的不工作
这更像是一个一般性的 XML 问题,但为了获得比我上面的评论更完整的答案,我参考 XSLT documentation regarding focus 说:
[Definition: The context item is the item currently being processed. An item (see [XDM 3.0]) is either an atomic value (such as an integer, date, or string), a node, or a function item. It changes whenever instructions such as xsl:apply-templates and xsl:for-each are used to process a sequence of items; each item in such a sequence becomes the context item while that item is being processed.] The context item is returned by the XPath expression . (dot).
因此,当您输入第一个 for-each 时,上下文已更改,您需要应用与该上下文相关的进一步选择,对于您的示例,从绝对路径更改为相对路径就足够了内循环的选择:
<xsl:for-each select="testparts/testpart">