Abhishek_Mishra
Abhishek_Mishra

Reputation: 4621

Facing issues in parsing xml with xslt

I am trying to parse a xml to generate a html report.The problematic section of the xml is as given

<failure message="Management changes link count is not 3$HiHello" type="junit.framework.AssertionFailedError">junit.framework.AssertionFailedError: Management changes link count is not 3$HiHelloJI
at CustomProjects.CommonTemplates.verifyManagementChanges(Unknown Source)
at CustomProjects.EmersonTest.testEmerson_VerifyManagementChanges(Unknown Source)
</failure>

The xslt written for parsing this is :

<xsl:choose>
        <xsl:when test="failure">
            <td>Failure</td>
            <td><xsl:apply-templates select="failure"/></td>
            <td><a href="ftp://10.32.1.66/seleniumScreenshotsFireFoxTest/{@name}.png" target="_blank">screenshot</a></td>
            <td><xsl:apply-templates select="failurelink"/></td>
        </xsl:when>
</xsl:choose>

<xsl:template match="failure">
  <xsl:call-template name="display-failures"/>
</xsl:template>

<xsl:template match="failurelink">
  <xsl:call-template name="display-failures-link"/>
</xsl:template>

<xsl:template name="display-failures">
  <xsl:param name="FailText" select="@message"/>
    <xsl:choose>
     <xsl:when test="not(@message)">N/A</xsl:when>
    <xsl:otherwise>
       <xsl:value-of select="substring-before($FailText,'$')"/>
    </xsl:otherwise>
</xsl:choose>
<!-- display the stacktrace -->
<code>
    <br/><br/>
    <xsl:call-template name="br-replace">
        <xsl:with-param name="word" select="."/>
    </xsl:call-template>
</code>
<!-- the later is better but might be problematic for non-21" monitors... -->
<!--pre><xsl:value-of select="."/></pre-->
</xsl:template>

<xsl:template name="display-failures-link">
<xsl:param name="linktext" select="@message"/>
 <xsl:choose>
    <xsl:when test="not(@message)">N/A</xsl:when>
    <xsl:otherwise>
       <xsl:value-of select="substring-after($linktext,'$')"/>
    </xsl:otherwise>
</xsl:choose>
<!-- display the stacktrace -->
<code>
    <br/><br/>
    <xsl:call-template name="br-replace">
        <xsl:with-param name="word" select="."/>
    </xsl:call-template>
</code>
<!-- the later is better but might be problematic for non-21" monitors... -->
<!--pre><xsl:value-of select="."/></pre-->
</xsl:template>

Here I am getting the desired result(The String before $ sign) from display-failures template but on calling display-failures-link I am getting nothing.(Should get the string after $ sign).I dont know whether the problem is with sunstring function or with something else.Kindly let me know what I am doing wrong here.

Any help is highly appreciated.

Upvotes: 0

Views: 209

Answers (1)

JLRishe
JLRishe

Reputation: 101748

The problem here is that you are trying to apply-templates on the XPath failurelink, but you don't have an element called <failurelink>, so this apply-templates isn't finding anything.

<xsl:apply-templates select="failurelink"/>

One way to apply two different templates on the same kind of element is to use modes:

<xsl:template match="failure">
  <xsl:call-template name="display-failures"/>
</xsl:template>

<xsl:template match="failure" mode="link">
  <xsl:call-template name="display-failures-link"/>
</xsl:template>

Then the area where you apply the templates would change to this:

<td>Failure</td>
<td><xsl:apply-templates select="failure"/></td>
<td><a href="ftp://10.32.1.66/seleniumScreenshotsFireFoxTest/{@name}.png" target="_blank">screenshot</a></td>
<td><xsl:apply-templates select="failure" mode="link"/></td>

But in your case, there's an even better approach. Just eliminate the second template, and do this:

Replace the whole <xsl:choose> with:

<xsl:apply-templates select="failure" />

Replace the first template you listed with:

<xsl:template match="failure">
   <td>Failure</td>
   <td><xsl:call-template name="display-failures"/></td>
   <td><a href="ftp://10.32.1.66/seleniumScreenshotsFireFoxTest/{../@name}.png" target="_blank">screenshot</a></td>
   <td><xsl:call-template name="display-failures-link"/></td>
</xsl:template>

And delete the second template you listed.

Upvotes: 3

Related Questions