我正在使用 XSLT 1.0。如果需要,我可以將 XSLT 版本更改為 2.0。
我有 XML 資料。錯誤訊息包裹在標簽周圍
<testsuites>
<testsuite>
<system-out>
<![CDATA[
[icbomTree, Basic test 1, 2] Should be A-700/1
Source: at Object.<anonymous> (http://localhost/s300/Areas/IC/Scripts/tests/testcases/ICBOM_Tree-qunit.js:818:16)
[icbomTree, Basic test 1, 3] Should be A-340/7
Source: at Object.<anonymous> (http://localhost/s300/Areas/IC/Scripts/tests/testcases/ICBOM_Tree-qunit.js:819:16)
[icbomTree, Basic test 1, 4] Expected 2 assertions, but 3 were run
Source: at Function.icbomTreeTestSuite.basic1 (http://localhost/s300/Areas/IC/Scripts/tests/testcases/ICBOM_Tree-qunit.js:814:45)
[icbomTree, Basic test 2, 2] Should be B-480/9
Source: at Object.<anonymous> (http://localhost/s300/Areas/IC/Scripts/tests/testcases/ICBOM_Tree-qunit.js:826:16)
[icbomTree, Basic test 2, 3] Should be E-990/7
Source: at Object.<anonymous> (http://localhost/s300/Areas/IC/Scripts/tests/testcases/ICBOM_Tree-qunit.js:827:16)
]]>
</system-out>
</testsuite>
</testsuites>
如何使用 XSLT 決議出每條錯誤訊息?每個錯誤訊息正好是兩行
我的嘗試如下:
<xsl:for-each select="system-out">
<tr>
<td><xsl:value-of select="[I can't do that because I cannot select the contents in CDATA using XPath]"/></td>
</tr>
</xsl:for-each>
最終結果應該是在表格的每一行中按順序顯示的錯誤訊息:
<tr>
<td>
[icbomTree, Basic test 1, 2] Should be A-700/1
Source: at Object.<anonymous> (http://localhost/s300/Areas/IC/Scripts/tests/testcases/ICBOM_Tree-qunit.js:818:16)
</td>
</tr>
<tr>
<td>
[icbomTree, Basic test 1, 3] Should be A-340/7
Source: at Object.<anonymous> (http://localhost/s300/Areas/IC/Scripts/tests/testcases/ICBOM_Tree-qunit.js:819:16)
</td>
</tr>
<tr>
<td>
[icbomTree, Basic test 1, 4] Expected 2 assertions, but 3 were run
Source: at Function.icbomTreeTestSuite.basic1 (http://localhost/s300/Areas/IC/Scripts/tests/testcases/ICBOM_Tree-qunit.js:814:45)
</td>
</tr>
<tr>
<td>
[icbomTree, Basic test 2, 2] Should be B-480/9
Source: at Object.<anonymous> (http://localhost/s300/Areas/IC/Scripts/tests/testcases/ICBOM_Tree-qunit.js:826:16)
</td>
</tr>
<tr>
<td>
[icbomTree, Basic test 2, 3] Should be E-990/7
Source: at Object.<anonymous> (http://localhost/s300/Areas/IC/Scripts/tests/testcases/ICBOM_Tree-qunit.js:827:16)
</td>
</tr>
uj5u.com熱心網友回復:
我相信這會回傳預期的結果:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/testsuites">
<table border="1">
<xsl:for-each select="testsuite">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="system-out"/>
</xsl:call-template>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="' '"/>
<xsl:variable name="adj-text" select="concat($text, $delimiter)" />
<xsl:variable name="token">
<xsl:value-of select="substring-before($adj-text, $delimiter)"/>
<br/>
<xsl:value-of select="substring-before(substring-after($adj-text, $delimiter), $delimiter)"/>
</xsl:variable>
<xsl:if test="normalize-space($token)">
<tr>
<td>
<xsl:copy-of select="$token"/>
</td>
</tr>
</xsl:if>
<xsl:variable name="tail" select="substring-after(substring-after($text, $delimiter), $delimiter)"/>
<xsl:if test="$tail">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="$tail"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/459339.html
下一篇:構建混合文本/標簽元素
