Sosi
Sosi

Reputation: 2578

Trying to use an XSLT for an XML in ASP.NET

i have the following xslt sheet:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:variable name="nhits" select="Answer[@nhits]"></xsl:variable>
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/">
    <div>
      <xsl:call-template name="resultsnumbertemplate"/>
    </div>
  </xsl:template>  
  <xsl:template name="resultsnumbertemplate">
    <xsl:value-of select="$nhits"/> matches found
  </xsl:template>  
</xsl:stylesheet>

And this is the xml that im trying to mix with the previous xslt:

<Answer xmlns="exa:com.exalead.search.v10" context="n%3Dsl-ocu%26q%3Dlavadoras" last="9" estimated="false" nmatches="219" nslices="0" nhits="219" start="0">
  <time>
    <Time interrupted="false" overall="32348" parse="0" spell="0" exec="1241" synthesis="15302" cats="14061" kwds="14061">
      <sliceTimes>15272 </sliceTimes>
    </Time>
  </time>    
</Answer>

Im using a xslcompiledtransform and that's working fine:

XslCompiledTransform transformer = new XslCompiledTransform();

transformer.Load(HttpContext.Current.Server.MapPath("xslt\\" + requestvariables["xslsheet"].ToString()));

transformer.Transform(xmlreader, null, writer);

My problems comes when im trying to put into a variable the "nhits" attribute value placed on the Answer element, but i'm not rendering anything using my xslt sheet.

Do you know what could be the cause?

Could be the xmlns attribute in my xml file?

Thanks in advance.

Best Regards.

Jose

Upvotes: 0

Views: 176

Answers (2)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195982

Your variable should be select="Answer/@nhits"

Your currect xpath of "Answer[@nhits]" tries to select Answer element that has an attribute named nhits..

Upvotes: 1

Tomalak
Tomalak

Reputation: 338158

Your immediate problem is that your XPath is wrong. Try

<xsl:variable name="nhits" select="/Answer/@nhits" />

However, I suggest a change to get rid of the variable altogether, you don't need it.

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="html" indent="yes"/>

  <xsl:template match="Answer">
    <div>
      <xsl:value-of select="@nhits"/>
      <xsl:text> matches found</xsl:text>
    </div>
  </xsl:template>  

</xsl:stylesheet>

Upvotes: 1

Related Questions