使用 XSLT 将 CDATA 提取到 html table
Extract CDATA into html table using XSLT
我有这个 XML 结构:
<testsuites>
<testsuite>
<system-out>
<![CDATA[
[pmRevisedEstOptionalField, canPmRevisedEstDeleteOptionalFieldLine, 1] Before row deletion
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
[pmRevisedEstOptionalField, canPmRevisedEstAddOptionalFieldLine, 3] VendorName desc. match
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:72:18)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunit.js:163:4)
[pmRevisedEstOptionalField, canPmRevisedEstAddOptionalFieldLine, 4] VendorName match
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:72:18)
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 1] VendorName desc. match
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:72:18)
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 2] Column dropdown list Optional Field selected
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 3] Error message for adding existing optional field
Source: at addOptionalField (http://localhost/s300/Areas/PM/Scripts/tests/testcases/PMRevisedEst_OptionalField-qunit.js:1099:20)
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 4] duplicate row add
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
]]>
</system-out>
</testsuite>
</testsuites>
我的目标是使用 XSLT 解析每条错误消息。每条错误消息都从 [
字符开始,并在下一个 [
.
之前以换行符结束
我的尝试如下:目前,解析将每条错误消息视为恰好有 2 行。如果消息超过 2 行,消息将无法正确解析。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" 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)"/>
<xsl:value-of select="$delimiter"/>
<xsl:value-of select="substring-before(substring-after($adj-text, $delimiter), $delimiter)"/>
</xsl:variable>
<xsl:if test="normalize-space($token)">
<td style="color:#550000" colspan="3">
<!-- something needs to be changed here -->
<xsl:value-of select="substring-before($token, $delimiter)"/><br/><xsl:value-of select="substring-after($token, $delimiter)"/>
</td>
</xsl:if>
<xsl:variable name="tail" select="substring-after(substring-after($text, $delimiter), $delimiter)"/>
<xsl:if test="$tail>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="$tail"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
预期结果应按顺序显示错误消息:
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstDeleteOptionalFieldLine, 1] Before row deletion
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
</td>
</tr>
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstAddOptionalFieldLine, 3] VendorName desc. match
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:72:18)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunit.js:163:4)
</td>
</tr>
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstAddOptionalFieldLine, 4] VendorName match
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:72:18)
</td>
</tr>
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 1] VendorName desc. match
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:72:18)
</td>
</tr>
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 2] Column dropdown list Optional Field selected
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
</td>
</tr>
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 3] Error message for adding existing optional field
Source: at addOptionalField (http://localhost/s300/Areas/PM/Scripts/tests/testcases/PMRevisedEst_OptionalField-qunit.js:1099:20)
</td>
</tr>
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 4] duplicate row add
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
</td>
</tr>
AFAICT,您显示的结果可以使用:
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="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:if test="normalize-space($token)">
<tr>
<td>
<xsl:value-of select="concat($delimiter, $token)"/>
</td>
</tr>
</xsl:if>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
我有这个 XML 结构:
<testsuites>
<testsuite>
<system-out>
<![CDATA[
[pmRevisedEstOptionalField, canPmRevisedEstDeleteOptionalFieldLine, 1] Before row deletion
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
[pmRevisedEstOptionalField, canPmRevisedEstAddOptionalFieldLine, 3] VendorName desc. match
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:72:18)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunit.js:163:4)
[pmRevisedEstOptionalField, canPmRevisedEstAddOptionalFieldLine, 4] VendorName match
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:72:18)
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 1] VendorName desc. match
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:72:18)
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 2] Column dropdown list Optional Field selected
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 3] Error message for adding existing optional field
Source: at addOptionalField (http://localhost/s300/Areas/PM/Scripts/tests/testcases/PMRevisedEst_OptionalField-qunit.js:1099:20)
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 4] duplicate row add
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
]]>
</system-out>
</testsuite>
</testsuites>
我的目标是使用 XSLT 解析每条错误消息。每条错误消息都从 [
字符开始,并在下一个 [
.
我的尝试如下:目前,解析将每条错误消息视为恰好有 2 行。如果消息超过 2 行,消息将无法正确解析。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" 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)"/>
<xsl:value-of select="$delimiter"/>
<xsl:value-of select="substring-before(substring-after($adj-text, $delimiter), $delimiter)"/>
</xsl:variable>
<xsl:if test="normalize-space($token)">
<td style="color:#550000" colspan="3">
<!-- something needs to be changed here -->
<xsl:value-of select="substring-before($token, $delimiter)"/><br/><xsl:value-of select="substring-after($token, $delimiter)"/>
</td>
</xsl:if>
<xsl:variable name="tail" select="substring-after(substring-after($text, $delimiter), $delimiter)"/>
<xsl:if test="$tail>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="$tail"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
预期结果应按顺序显示错误消息:
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstDeleteOptionalFieldLine, 1] Before row deletion
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
</td>
</tr>
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstAddOptionalFieldLine, 3] VendorName desc. match
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:72:18)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunit.js:163:4)
</td>
</tr>
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstAddOptionalFieldLine, 4] VendorName match
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:72:18)
</td>
</tr>
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 1] VendorName desc. match
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
at Function.checkGridCell (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:72:18)
</td>
</tr>
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 2] Column dropdown list Optional Field selected
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
</td>
</tr>
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 3] Error message for adding existing optional field
Source: at addOptionalField (http://localhost/s300/Areas/PM/Scripts/tests/testcases/PMRevisedEst_OptionalField-qunit.js:1099:20)
</td>
</tr>
<tr>
<td>
[pmRevisedEstOptionalField, canPmRevisedEstAddExistingOptionalFieldLine, 4] duplicate row add
Source: at Function.checkAssert (http://localhost/s300/Areas/Core/Scripts/1js/qunitLibrary.js:76:20)
</td>
</tr>
AFAICT,您显示的结果可以使用:
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="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:if test="normalize-space($token)">
<tr>
<td>
<xsl:value-of select="concat($delimiter, $token)"/>
</td>
</tr>
</xsl:if>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>