XSLT:仅选择最后一行(XSLT : Select only the last row)

编程入门 行业动态 更新时间:2024-10-10 11:26:02
XSLT:仅选择最后一行(XSLT : Select only the last row)

我想选择并显示特定的行。 让我先向您展示我的xml文件:

<?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>

步骤是TestCases的一部分,这意味着一个TestCase包含几个步骤,但步骤和TestCase是兄弟姐妹。 所以首先我用这个xsl文件在xml文件中下了一个命令:

<?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>

现在我想只选择每个TestCase的最后一步(这里是步骤3和6)并显示是否有错误,如下:

<table> <tr> <td>PA-343</td> <td>Error</td> </tr> <tr> <td>PA-450</td> <td>OK</td> </tr> </table>

但问题是我正在使用xsl:for-each和密钥,我不知道我是否还需要在xsl:for-each节点中工作,或者如果我不得不考虑没有密钥的解决方案。

谢谢您的回答

I would like to select and display specific rows. Let me first show you my xml file :

<?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>

Steps are part of TestCases, that means one TestCase contains several steps, but steps and TestCases are siblings. So first I have put an order in the xml file with this xsl file :

<?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>

Which gives me this 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>

Now I want to select only the last step of each TestCase (here steps 3 and 6) and display if there were an error or not like this :

<table> <tr> <td>PA-343</td> <td>Error</td> </tr> <tr> <td>PA-450</td> <td>OK</td> </tr> </table>

But the problem is that I am using a xsl:for-each with the key and I don't know if I still have to work in the xsl:for-each node or if I have to think about a solution without the key.

Thank you for your answers

最满意答案

如果您只想选择每个测试用例的“最后一步”,请更改第一个xsl:for-each以包含标识此类步骤的条件:

<xsl:for-each select="TestLogItem[contains(Message, 'TestCase')]">

然后,要检查该步骤是否发生任何错误,请不要使用内部xsl:for-each ,而是使用xsl:choose的键xsl:choose检查是否有任何步骤出错

<xsl:when test="key('k',@id)[contains(TypeDescription, 'Error')]">

试试这个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>

If you only wanted to select the "last step" of each test-case, change the first xsl:for-each to include the condition that identifies such a step:

<xsl:for-each select="TestLogItem[contains(Message, 'TestCase')]">

Then, to check if any errors occurred for that step, don't have the inner xsl:for-each, instead use the key in the xsl:choose to check if any of the steps have the error

<xsl:when test="key('k',@id)[contains(TypeDescription, 'Error')]">

Try this 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,<xsl,select,电脑培训,计算机培训,IT培训"/> <meta name="descriptio

本文发布于:2023-08-07 18:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465323.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:XSLT   Select   row

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!