views:

94

answers:

1

Hi, I have to write a very simple code in xsl: IF column=0 if result = .34 set background color to green and write $result, but if result = 0.10 set background color to white and write the word "QQQ" and what doesn't work is:

<xsl:if test="$result  = 0.35 and $column = 0">
    <xsl:attribute name='background-color'>#669933</xsl:attribute>
    <xsl:value-of select="result"/>      
</xsl:if>

<xsl:if test="$result = 0.10">
    <xsl:value-of select="QQQ"/>
</xsl:if>

any suggestions? thanks in advance

+1  A: 
<xsl:if test="$result  = 0.35 and $column = 0">    
    <xsl:attribute name='background-color'>#669933</xsl:attribute>

    <xsl:value-of select="result"/>          
</xsl:if>    

<xsl:if test="$result = 0.10">    
    <xsl:value-of select="QQQ"/>    
</xsl:if>

You have committed exactly two errors in the code above.

Here is the corrected version:

 <xsl:if test="$result  = 0.35 and $column = 0">
   <xsl:attribute name='background-color'>#669933</xsl:attribute>
   <xsl:value-of select="$result"/>
 </xsl:if>

 <xsl:if test="$result = 0.10">
   <xsl:value-of select="'QQQ'"/>
 </xsl:if>

The errors are:

  1. result means the elements named result that are children of the context node. You want the <xsl:variable> named result. By definition the name of any referenced <xsl:variable> should be prefixed by the $ character.

  2. <xsl:value-of select="QQQ"/> selects all children of the current node named QQQ and outputs the string value of the first of them. You want just the string 'QQQ' to be produced. By definition, to distinguish a string from a name, the string must be enclosed in quotes or in apostrophes.

Dimitre Novatchev
There seems to be an error in your corrected version, it contains the string "enter code here". Also, is there any need for `xsl:value-of` in this case? Why not insert "QQQ" as literal text, eventually wrapped in `xsl:text` to avoid whitespace issues?
markusk
@markusk: This was the code originally provided by the OP -- I just corrected the mistakes.
Dimitre Novatchev
@markusk: There is no whitespace issue here. Because the provided code is just a snippet we don't know about the context it is used in. Maybe what would seem as "whitespace issue" (not present in this case) would be something intended and desirable in the larger context??? When we don't know the context we should make no assumptions what is right and what is wrong -- leave this to the OP. :)
Dimitre Novatchev
thanks, I am sure you're right, but it still doesn't work. I am thinking now, that my problem might be passing the result as a paramter from the other part... Just wanted to check: I could say: "AResult = 0.35 or AResult = 0.10" using "or" for or? I know that I used "and" successfully in the other place, so I assumed I could use "or" here. thanks!
Elena
@Dimitre: I didn't mean to imply that there were any whitespace issues in your code, sorry about the vague comment. I just meant that if `<xsl:value-of select="'QQQ'"/>` was replaced with `QQQ`, one would need to be careful with surrounding whitespace, or use `<xsl:text>QQQ</xsl:text>` instead.
markusk
@Elena: If you have additional issues, you need to show the source XML (minimal document that still illustrates the problem) and the complete XSLT code (minimal stylesheet(s) that still illustrate the problem). You need to explain what the transformation is expected to do, what are the actual results from it and where you think there is a problem. Don't let us in guess mode, please. :)
Dimitre Novatchev
thanks and I will next time around. For now I think I could address the issue in ZEN, which uses <xsl:template>... Could you recommend some good xsl tutorial, like for dummies?
Elena
@Elena: See this: http://stackoverflow.com/questions/339930/any-good-xslt-tutorial-book-blog-site-online/341589#341589
Dimitre Novatchev