2

I'm trying to parse an xml file output from googletest to display the results in a pretty way in an html page. I have had a fair crack at it and have a working solution. Only one thing left to do. Currently each class has a header and each function+test is displayed underneath, I'm looking for a way to extract the function name from the xml attribute to have it as its own sub-heading. Example below


Current Output

Class Name
functionName_testName
functionName_test2Name
function2Name_testName

Desired Output

Class Name
functionName
testName
test2Name
function2Name
testName

XML To be Parsed

<testsuite name="testPacketProcessor" tests="27" failures="0" disabled="0" errors="0" time="36.875">
    <testcase name="testInitialise_IpNotSet" status="run" time="0" classname="testPacketProcessor" />
    <testcase name="testInitialise_GoodIp" status="run" time="2.046" classname="testPacketProcessor" />
    <testcase name="testInitialise_BadIp" status="run" time="0.032" classname="testPacketProcessor" />
</testsuite>

XSL Currently Implemented

<xsl:for-each select="testsuites/testsuite">
    <div class="testHeading">
        <span><xsl:value-of select="substring-after(@name,'test')"/></span>
    </div>
    <xsl:for-each select="testcase">
    <div class="testReport">
        <xsl:if test="not(starts-with(@name,'DISABLED'))"><xsl:value-of select="substring-after(@name,'test')"/></xsl:if>
        <xsl:if test="starts-with(@name,'DISABLED')"><xsl:value-of select="substring-after(@name,'DISABLED_test')"/></xsl:if>
        <xsl:text> - </xsl:text>
        <xsl:if test="starts-with(@status,'run')">
            <xsl:if test="failure"><xsl:text>Fail</xsl:text></xsl:if>
            <xsl:if test= "not(failure)"><xsl:text>Pass</xsl:text></xsl:if>
        </xsl:if>
        <xsl:if test="starts-with(@status,'not')"><xsl:text>Disabled</xsl:text></xsl:if>
    </div>
    </xsl:for-each>
</xsl:for-each>

Using the above xml as an example I would like a subheading of "Initialise" under the packet processor heading with each test doucumented under the subheading

Any advice on the best way to go about this would be much appreciated. I've looked at xsl variables but cannot fathom how to deal with not being able to change the value. Have also read a little on xsl templates and recursion but I'm not sure how that would work for this situation.

Thanks in advance

Dougie

1
  • Good question, +1. See my answer for a complete, short and easy solution. :) Commented Apr 12, 2011 at 13:02

1 Answer 1

1

This transformation produces the subheading and name for each testcase:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="testcase">
  Subheading: <xsl:value-of select=
   "substring-before(substring-after(@name, 'test'),
                     '_'
                     )
   "/>
   Name: <xsl:value-of select="substring-after(@name, '_')"/>
   <xsl:text>&#xA;</xsl:text>
 </xsl:template>
</xsl:stylesheet>

When applied on the provided XML document:

<testsuites>
    <testsuite name="testPacketProcessor" tests="27" failures="0" disabled="0" errors="0" time="36.875">
        <testcase name="testInitialise_IpNotSet" status="run" time="0" classname="testPacketProcessor" />
        <testcase name="testInitialise_GoodIp" status="run" time="2.046" classname="testPacketProcessor" />
        <testcase name="testInitialise_BadIp" status="run" time="0.032" classname="testPacketProcessor" />
    </testsuite>
</testsuites>

the wanted, correct result is produced:

  Subheading: Initialise
   Name: IpNotSet

  Subheading: Initialise
   Name: GoodIp

  Subheading: Initialise
   Name: BadIp

Explanation: Using the standard XPath functions substring-before() and substring-after()

Edit: In a comment the OP clarified that he wanted grouping by function (tired of bad questions ?):

This transformation performs the wanted grouping:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="kTestsByFunction" match="testcase"
     use="substring-before(substring-after(@name, 'test'),
                             '_'
                              )"
  />

    <xsl:template match=
     "testcase
     [generate-id()
   =
    generate-id(key('kTestsByFunction',
                    substring-before(substring-after(@name, 'test'),
                                     '_'
                                      )
                    )[1]
               )
      ]
     ">
    Subheading: 
        <xsl:value-of select=
        "substring-before(substring-after(@name, 'test'),
                          '_'
                          )
     "/>
    <xsl:for-each select=
     "key('kTestsByFunction',
           substring-before(substring-after(@name, 'test'),
                                '_'
                                )
         )
     ">
      Name:
           <xsl:value-of select="substring-after(@name, '_')"/>
           <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

and produces the wanted result:

    Subheading: 
        Initialise
  Name:
           IpNotSet

  Name:
           GoodIp

  Name:
           BadIp
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Dimitre, I had considered this but was hoping that there was a way where all tests for the initialise function were shown under the same subheading. Any ideas?
@dougie: Remember always to show the wanted output -- this will save you and your readers a lot of time. Edit your question and explain that you need the tests to be grouped by function. Also, show the wanted output. Then read about "Muenchian grouping" -- there are a lot of questions and excellent answers.
Thaks again, managed to get it working. Marked your answer as a solution. I had included the wanted output under the desired output section of my original comment, maybe it wasnt too clear. Muenchian grouping looks like a big topic but there are some great tutorials and Q+As out there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.