我是XSLT和XPATH的新手。我试图解析所有RELEASE_VERSION_NUMBER值并为每个唯一值生成一列(删除重复项)。然后,每个ROW的想法是在与列标题对应的列上写入EXECUTION_STATUS_NAME。
XML:
<Report>
<RESULTS>
<ROW>
<TEST_RUN_ID>338</TEST_RUN_ID>
<TEST_CASE_ID>202</TEST_CASE_ID>
<NAME>Test Case 1</NAME>
<EXECUTION_STATUS_NAME>Failed</EXECUTION_STATUS_NAME>
<RELEASE_VERSION_NUMBER>1.0.1</RELEASE_VERSION_NUMBER>
</ROW>
<ROW>
<TEST_RUN_ID>340</TEST_RUN_ID>
<TEST_CASE_ID>202</TEST_CASE_ID>
<NAME>Test Case 1</NAME>
<EXECUTION_STATUS_NAME>Failed</EXECUTION_STATUS_NAME>
<RELEASE_VERSION_NUMBER>1.0.2</RELEASE_VERSION_NUMBER>
</ROW>
<ROW>
<TEST_RUN_ID>342</TEST_RUN_ID>
<TEST_CASE_ID>202</TEST_CASE_ID>
<NAME>Test Case 1</NAME>
<EXECUTION_STATUS_NAME>Passed</EXECUTION_STATUS_NAME>
<RELEASE_VERSION_NUMBER>2.0.1</RELEASE_VERSION_NUMBER>
</ROW>
<ROW>
<TEST_RUN_ID>349</TEST_RUN_ID>
<TEST_CASE_ID>202</TEST_CASE_ID>
<NAME>Test Case 1</NAME>
<EXECUTION_STATUS_NAME>Passed</EXECUTION_STATUS_NAME>
<RELEASE_VERSION_NUMBER>2.0.1</RELEASE_VERSION_NUMBER>
</ROW>
<ROW>
<TEST_RUN_ID>352</TEST_RUN_ID>
<TEST_CASE_ID>202</TEST_CASE_ID>
<NAME>Test Case 1</NAME>
<EXECUTION_STATUS_NAME>Failed</EXECUTION_STATUS_NAME>
<RELEASE_VERSION_NUMBER>2.0.2</RELEASE_VERSION_NUMBER>
</ROW>
<ROW>
<TEST_RUN_ID>341</TEST_RUN_ID>
<TEST_CASE_ID>203</TEST_CASE_ID>
<NAME>Test Case 2</NAME>
<EXECUTION_STATUS_NAME>Failed</EXECUTION_STATUS_NAME>
<RELEASE_VERSION_NUMBER>1.0.2</RELEASE_VERSION_NUMBER>
</ROW>
<ROW>
<TEST_RUN_ID>343</TEST_RUN_ID>
<TEST_CASE_ID>203</TEST_CASE_ID>
<NAME>Test Case 2</NAME>
<EXECUTION_STATUS_NAME>Passed</EXECUTION_STATUS_NAME>
<RELEASE_VERSION_NUMBER>2.0.1</RELEASE_VERSION_NUMBER>
</ROW>
<ROW>
<TEST_RUN_ID>350</TEST_RUN_ID>
<TEST_CASE_ID>203</TEST_CASE_ID>
<NAME>Test Case 2</NAME>
<EXECUTION_STATUS_NAME>Passed</EXECUTION_STATUS_NAME>
<RELEASE_VERSION_NUMBER>2.0.1</RELEASE_VERSION_NUMBER>
</ROW>
<ROW>
<TEST_RUN_ID>351</TEST_RUN_ID>
<TEST_CASE_ID>203</TEST_CASE_ID>
<NAME>Test Case 2</NAME>
<EXECUTION_STATUS_NAME>Failed</EXECUTION_STATUS_NAME>
<RELEASE_VERSION_NUMBER>2.0.1</RELEASE_VERSION_NUMBER>
</ROW>
<ROW>
<TEST_RUN_ID>353</TEST_RUN_ID>
<TEST_CASE_ID>203</TEST_CASE_ID>
<NAME>Test Case 2</NAME>
<EXECUTION_STATUS_NAME>Passed</EXECUTION_STATUS_NAME>
<RELEASE_VERSION_NUMBER>2.0.2</RELEASE_VERSION_NUMBER>
</ROW>
</RESULTS>
</Report>
我目前拥有的XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:key name="releases" match="ROW" use="RELEASE_VERSION_NUMBER" />
<xsl:template match="/RESULTS">
<table class="DataGrid">
<tr>
<th>TEST RUN</th>
<th>TEST CASE</th>
<th>NAME</th>
<th>STATUS</th>
<xsl:for-each select="ROW[count(. | key('releases', RELEASE_VERSION_NUMBER)[1]) = 1]">
<xsl:sort select="RELEASE_VERSION_NUMBER" order="ascending"/>
<th><xsl:value-of select="RELEASE_VERSION_NUMBER"/></th>
</xsl:for-each>
</tr>
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="ROW">
<tr>
<td><xsl:value-of select="TEST_RUN_ID"/></td>
<td><xsl:value-of select="TEST_CASE_ID"/></td>
<td><xsl:value-of select="NAME"/></td>
<td><xsl:value-of select="EXECUTION_STATUS_NAME"/></td>
<td><xsl:value-of select="RELEASE_VERSION_NUMBER"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
输出现在看起来像这样:
<html>
<body>
<table class="DataGrid">
<tr>
<th>TEST RUN</th><th>TEST CASE</th><th>NAME</th><th>STATUS</th><th>1.0.1</th><th>1.0.2</th><th>2.0.1</th><th>2.0.2</th>
</tr>
<tr>
<td>338</td><td>202</td><td>Test Case 1</td><td>Failed</td><td>1.0.1</td>
</tr>
<tr>
<td>340</td><td>202</td><td>Test Case 1</td><td>Failed</td><td>1.0.2</td>
</tr>
<tr>
<td>342</td><td>202</td><td>Test Case 1</td><td>Passed</td><td>2.0.1</td>
</tr>
<tr>
<td>349</td><td>202</td><td>Test Case 1</td><td>Passed</td><td>2.0.1</td>
</tr>
<tr>
<td>352</td><td>202</td><td>Test Case 1</td><td>Failed</td><td>2.0.2</td>
</tr>
<tr>
<td>341</td><td>203</td><td>Test Case 2</td><td>Failed</td><td>1.0.2</td>
</tr>
<tr>
<td>343</td><td>203</td><td>Test Case 2</td><td>Passed</td><td>2.0.1</td>
</tr>
<tr>
<td>350</td><td>203</td><td>Test Case 2</td><td>Passed</td><td>2.0.1</td>
</tr>
<tr>
<td>351</td><td>203</td><td>Test Case 2</td><td>Failed</td><td>2.0.1</td>
</tr>
<tr>
<td>353</td><td>203</td><td>Test Case 2</td><td>Passed</td><td>2.0.2</td>
</tr>
</table>
</body>
</html>
预期输出如下(删除STATUS列后):
<html>
<body>
<table class="DataGrid">
<tr>
<th>TEST RUN</th><th>TEST CASE</th><th>NAME</th><th>1.0.1</th><th>1.0.2</th><th>2.0.1</th><th>2.0.2</th>
</tr>
<tr>
<td>338</td><td>202</td><td>Test Case 1</td><td>Failed</td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td>340</td><td>202</td><td>Test Case 1</td><td></td><td>Failed</td><td></td><td></td>
</tr>
<tr>
<td>342</td><td>202</td><td>Test Case 1</td><td></td><td></td><td>Passed</td><td></td>
</tr>
<tr>
<td>349</td><td>202</td><td>Test Case 1</td><td></td><td></td><td>Passed</td><td></td>
</tr>
<tr>
<td>352</td><td>202</td><td>Test Case 1</td><td></td><td></td><td></td><td>Failed</td>
</tr>
<tr>
<td>341</td><td>203</td><td>Test Case 2</td><td></td><td>Failed</td><td></td><td></td>
</tr>
<tr>
<td>343</td><td>203</td><td>Test Case 2</td><td></td><td></td><td>Passed</td><td></td>
</tr>
<tr>
<td>350</td><td>203</td><td>Test Case 2</td><td></td><td></td><td>Passed</td><td></td>
</tr>
<tr>
<td>351</td><td>203</td><td>Test Case 2</td><td></td><td></td><td>Failed</td><td></td>
</tr>
<tr>
<td>353</td><td>203</td><td>Test Case 2</td><td></td><td></td><td></td><td>Passed</td>
</tr>
</table>
</body>
</html>
最终目标是删除TEST_RUN_ID并仅显示唯一的TEST_CASE_ID,其中最新的TEST_RUN_ID值确定为某些RELEASE_VERSION_NUMBER显示哪个EXECUTION_STATUS_NAME。然后输出看起来像这样:
<html>
<body>
<table class="DataGrid">
<tr>
<th>TEST CASE</th><th>NAME</th><th>1.0.1</th><th>1.0.2</th><th>2.0.1</th><th>2.0.2</th>
</tr>
<tr>
<td>202</td><td>Test Case 1</td><td>Failed</td><td>Failed</td><td>Passed</td><td>Failed</td>
</tr>
<tr>
<td>203</td><td>Test Case 2</td><td>Not Run</td><td>Failed</td><td>Failed</td><td>Passed</td>
</tr>
</table>
</body>
</html>
但我仍然坚持如何在正确的列上发布版本。请帮我找一个合理的方法。
答案 0 :(得分:0)
我认为您可以存储一次唯一的发布版本号,然后生成相应的列:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="exsl"
version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:key name="releases" match="ROW" use="RELEASE_VERSION_NUMBER" />
<xsl:variable name="unique-releases-rtf">
<xsl:for-each select="//ROW[count(. | key('releases', RELEASE_VERSION_NUMBER)[1]) = 1]">
<xsl:sort select="RELEASE_VERSION_NUMBER" order="ascending"/>
<xsl:copy-of select="RELEASE_VERSION_NUMBER"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="unique-releases" select="exsl:node-set($unique-releases-rtf)/*"/>
<xsl:template match="RESULTS">
<table class="DataGrid">
<tr>
<th>TEST RUN</th>
<th>TEST CASE</th>
<th>NAME</th>
<xsl:for-each select="$unique-releases">
<th><xsl:value-of select="."/></th>
</xsl:for-each>
</tr>
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="ROW">
<tr>
<td><xsl:value-of select="TEST_RUN_ID"/></td>
<td><xsl:value-of select="TEST_CASE_ID"/></td>
<td><xsl:value-of select="NAME"/></td>
<xsl:variable name="row" select="."/>
<xsl:for-each select="$unique-releases">
<td>
<xsl:if test="$row/RELEASE_VERSION_NUMBER = .">
<xsl:value-of select="$row/EXECUTION_STATUS_NAME"/>
</xsl:if>
</td>
</xsl:for-each>
</tr>
</xsl:template>
</xsl:stylesheet>
唯一需要注意的是,XSLT 1.0使变量成为首先需要转换为节点集的结果树片段,我使用exsl:node-set
来实现这一点,因为它是大多数XSLT 1.0实现中支持的函数,虽然对于Microsoft MSXML或XslTransform,您需要使用msxsl:node-set
代替。