Home > Blockchain >  Extract CDATA into html table using XSLT
Extract CDATA into html table using XSLT

Time:04-20

I have this XML structure:

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

My goal is to parse out each error message using XSLT. Each error message starts from the [ character and ends with a line break just before the next [.

My attempt is the following: currently, the parsing treats each error message to have exactly 2 lines. If the message have over 2 lines, the message will not parse correctly.

<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="'&#10;'"/>
        <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>

The expected result should display the error messages in order:

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

CodePudding user response:

AFAICT, the result you show can be produced using:

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="'&#10;['"/>
    <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>
  • Related