views:

27

answers:

2

Hello,

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

+1  A: 

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>
Tomalak
Hello Tomalak, first of all thanks for your fast response.If i use the code that you gave me (the xsl sheet), the browser shows me all the elements, not only the "value of select" and matches found.Do you know what could be the cause?Thanks a lot.Best Regards.Jose.
Josemalive
@Jose: Yes. If there is only one `<Answer>` element and you are not interested in anything else from your XML, change `<xsl:template match="Answer">` to `<xsl:template match="/Answer">`
Tomalak
+1  A: 

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

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

Gaby
Hello Gaby, thanks for your response.The issue that im having now is that i can print all, or nothing. Not an specific element or attribute.Do you know why could be the cause?Thanks in advance.Best Regards.Jose
Josemalive
You mean `Answer[@nhits]`, because `Answer/[@nhits]` is a syntax error.
Tomalak
@Tomalak, correct.. i misread it ..
Gaby