简体   繁体   中英

Using xsl to parse xml output, use of variables?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM