我现在正在XSLT 1.0工作,我在正确分组时遇到了麻烦。我的XML无法更改,看起来像:
<Analysis>
<comment>Test 1</comment>
<Action>
<Result>Passed</Result>
</Action>
<Action>
<Result>Passed</Result>
</Action>
<Action>
<Result>Passed</Result>
</Action>
<comment>Start 2nd Phase</comment>
<Action>
<Result>Failed</Result>
</Action>
<Action>
<Result>Passed</Result>
</Action>
<comment>Test 2</comment>
<Action>
<Result>Failed</Result>
</Action>
<Action>
<Result>Failed</Result>
</Action>
<Action>
<Result>Passed</Result>
</Action>
<comment>Start 2nd Phase</comment>
<Action>
<Result>Failed</Result>
</Action>
<comment>Start 3rd Phase</comment>
<Action>
<Result>Failed</Result>
</Action>
<comment>Test 3</comment>
<Action>
<Result>Passed</Result>
</Action>
<Action>
<Result>Passed</Result>
</Action>
<Action>
<Result>Passed</Result>
</Action>
<Action>
<Result>Passed</Result>
</Action>
<Action>
<Result>Passed</Result>
</Action>
<comment>Test 4</comment>
<Action>
<Result>Failed</Result>
</Action>
<Action>
<Result>Failed</Result>
</Action>
<Action>
<Result>Passed</Result>
</Action>
<comment>Start 2nd Phase</comment>
<Action>
<Result>Failed</Result>
</Action>
<Action>
<Result>Passed</Result>
</Action>
</Analysis>
我当前拥有的代码将在测试下发现失败,但它只能完成一次。但是,现在它在每个评论下都可以做到,而不是每个评论中都有"测试"。
<xsl:key name="actions" match="Action" use="generate-id(preceding-sibling::comment[1])" />
<xsl:template match="/Analysis">
<table>
<xsl:for-each select="comment">
<tr>
<td>
<xsl:if test="starts-with(text(),'Test')">
<xsl:value-of select="."/>
</xsl:if>
</td>
</tr>
<xsl:if test="key('actions', generate-id())[Result='Failed']">
<tr>
<td>Failed</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>
有没有办法使"先前的sibling ::评论" [1]仅适用于以"测试"或包含"测试"开头的注释?有不同数量的非重要注释散布在重要信息中。就像上面的示例一样,我想看到一个像
的输出Test 1
Failed
Test 2
Failed
Test 3
Test 4
Failed
但是,我目前有代码
Test 1
Failed
Test 2
Failed
Failed
Failed
Test 3
Test 4
Failed
Failed
任何帮助将不胜感激。
您应该能够将谓词直接添加到xsl:key
...
<xsl:key
name="actions"
match="Action"
use="generate-id(preceding-sibling::comment[starts-with(normalize-space(),'Test')][1])"
/>
我还将删除xsl:if
并将测试放在xsl:for-each
上。这样,您就不会得到空白的行以忽略的评论。
完整示例...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key
name="actions"
match="Action"
use="generate-id(preceding-sibling::comment[starts-with(normalize-space(),'Test')][1])"
/>
<xsl:template match="/Analysis">
<table>
<xsl:for-each select="comment[starts-with(normalize-space(),'Test')]">
<tr>
<td>
<xsl:value-of select="."/>
</td>
</tr>
<xsl:if test="key('actions', generate-id())[Result='Failed']">
<tr>
<td>Failed</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>