Xml XSLT:仅选择最后一行
我想选择并显示特定的行。首先让我向您展示我的xml文件:Xml XSLT:仅选择最后一行,xml,xslt,Xml,Xslt,我想选择并显示特定的行。首先让我向您展示我的xml文件: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="TestLogTest.xsl" type="text/xsl"?> <TestLog> <TestLogItem id="0"> <Message>Zero</Message> <TypeD
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="TestLogTest.xsl" type="text/xsl"?>
<TestLog>
<TestLogItem id="0">
<Message>Zero</Message>
<TypeDescription>Normal</TypeDescription>
</TestLogItem>
<TestLogItem id="1">
<Message>One</Message>
<TypeDescription>Normal</TypeDescription>
</TestLogItem>
<TestLogItem id="2">
<Message>Two</Message>
<TypeDescription>Error</TypeDescription>
</TestLogItem>
<TestLogItem id="3">
<Message>Three</Message>
<TypeDescription>Normal</TypeDescription>
</TestLogItem>
<TestLogItem id="4">
<Message>******** TestCase PA-343 is finished *********</Message>
<TypeDescription>Normal</TypeDescription>
</TestLogItem>
<TestLogItem id="5">
<Message>Five</Message>
<TypeDescription>Normal</TypeDescription>
</TestLogItem>
<TestLogItem id="6">
<Message>Six</Message>
<TypeDescription>Normal</TypeDescription>
</TestLogItem>
<TestLogItem id="7">
<Message>******** TestCase PA-450 is finished *********</Message>
<TypeDescription>Normal</TypeDescription>
</TestLogItem>
</TestLog>
零
正常的
一个
正常的
两个
错误
三
正常的
********测试用例PA-343已完成*********
正常的
五
正常的
六
正常的
********测试用例PA-450已完成*********
正常的
步骤是测试用例的一部分,这意味着一个测试用例包含多个步骤,但步骤和测试用例是同级的。首先,我在xml文件中对这个xsl文件进行了排序:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="k" match="TestLogItem[string(Message)]" use="following-sibling::TestLogItem[contains(Message, 'TestCase')][1]/@id"/>
<xsl:template match="/TestLog">
<table border="2">
<th>Step</th>
<th>Test</th>
<th>Result</th>
<xsl:for-each select="TestLogItem">
<xsl:variable name="bingo" select="substring(Message, 19, 7)"/>
<xsl:variable name="position" select="@id"/>
<xsl:for-each select="key('k',@id)">
<tr>
<xsl:choose>
<xsl:when test="contains(TypeDescription, 'Error')">
<td bgcolor="#FF0000"><xsl:value-of select="@id"/></td>
<td bgcolor="#FF0000"><xsl:value-of select="$bingo"/></td>
<td bgcolor="#FF0000"> Error </td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="$bingo"/></td>
<td> OK </td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
步
试验
结果
错误
好啊
这给了我这个xml:
<?xml version="1.0" encoding="UTF-8"?>
<table border="2">
<th>Step</th>
<th>Test</th>
<th>Result</th>
<tr>
<td>0</td>
<td>PA-343</td>
<td>OK</td>
</tr>
<tr>
<td>1</td>
<td>PA-343</td>
<td>OK</td>
</tr>
<tr>
<td bgcolor="#FF0000">2</td>
<td bgcolor="#FF0000">PA-343</td>
<td bgcolor="#FF0000">Error</td>
</tr>
<tr>
<td>3</td>
<td>PA-343</td>
<td>OK</td>
</tr>
<tr>
<td>4</td>
<td>PA-450</td>
<td>OK</td>
</tr>
<tr>
<td>5</td>
<td>PA-450</td>
<td>OK</td>
</tr>
<tr>
<td>6</td>
<td>PA-450</td>
<td>OK</td>
</tr>
</table>
步
试验
结果
0
PA-343
好啊
1.
PA-343
好啊
2.
PA-343
错误
3.
PA-343
好啊
4.
PA-450
好啊
5.
PA-450
好啊
6.
PA-450
好啊
现在,我只想选择每个测试用例的最后一步(这里是步骤3和6),并显示是否有错误,如下所示:
<table>
<tr>
<td>PA-343</td>
<td>Error</td>
</tr>
<tr>
<td>PA-450</td>
<td>OK</td>
</tr>
</table>
PA-343
错误
PA-450
好啊
但问题是,我正在使用xsl:for-each和键,我不知道我是否仍然必须使用xsl:for-each节点,或者是否必须考虑一个没有键的解决方案
感谢您的回答如果您只想选择每个测试用例的“最后一步”,请更改第一个
xsl:for each
,以包括标识此类步骤的条件:
<xsl:for-each select="TestLogItem[contains(Message, 'TestCase')]">
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="k" match="TestLogItem[string(Message)]" use="following-sibling::TestLogItem[contains(Message, 'TestCase')][1]/@id"/>
<xsl:template match="/TestLog">
<table border="2">
<th>Step</th>
<th>Test</th>
<th>Result</th>
<xsl:for-each select="TestLogItem[contains(Message, 'TestCase')]">
<xsl:variable name="bingo" select="substring(Message, 19, 7)"/>
<tr>
<xsl:choose>
<xsl:when test="key('k',@id)[contains(TypeDescription, 'Error')]">
<td bgcolor="#FF0000"><xsl:value-of select="$bingo"/></td>
<td bgcolor="#FF0000"> Error </td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="$bingo"/></td>
<td> OK </td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
步
试验
结果
错误
好啊
太完美了!我不知道我是否可以把钥匙放在xsl:choose中。非常感谢您快速而完美的回答!是否也可以显示“错误”消息,但仍显示最后一步?是的,您可以执行
您可以使用xsl:for each
<代码>太好了,再次感谢您!我(又)没走多远,但和往常一样,细节让我抓狂
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="k" match="TestLogItem[string(Message)]" use="following-sibling::TestLogItem[contains(Message, 'TestCase')][1]/@id"/>
<xsl:template match="/TestLog">
<table border="2">
<th>Step</th>
<th>Test</th>
<th>Result</th>
<xsl:for-each select="TestLogItem[contains(Message, 'TestCase')]">
<xsl:variable name="bingo" select="substring(Message, 19, 7)"/>
<tr>
<xsl:choose>
<xsl:when test="key('k',@id)[contains(TypeDescription, 'Error')]">
<td bgcolor="#FF0000"><xsl:value-of select="$bingo"/></td>
<td bgcolor="#FF0000"> Error </td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="$bingo"/></td>
<td> OK </td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>