Home > Blockchain >  How to extract CDATA and put the contents into a table using XSLT
How to extract CDATA and put the contents into a table using XSLT

Time:04-19

I am using XSLT 1.0. I can change the XSLT version to 2.0 if necessarily.

I have the XML data. The error messages is wrapped around the tag

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

How do I parse out each error message using XSLT? Each error message is exactly two lines

My attempt as follows:

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

The end result should be the error messages being displayed in order in each row of a table:

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

CodePudding user response:

I believe this returns the expected result:

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="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>
  • Related