3

I have to write a simple condition in XSL:

IF column=0 AND IF result = .35 
    set background color to green and write $result
ELSE IF result = 0.10 
    set background color to white and write the word "QQQ"

I have tried this but it doesn't work:

<xsl:param name="result"  />
    <xsl:param name="column" />    

    <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?

vcsjones
  • 138,677
  • 31
  • 291
  • 286
Elena
  • 31
  • 1
  • 2
  • 2
    Could you post a snippet of the XML as well? That may help. – bryanjonker Apr 20 '10 at 14:49
  • Good Question (+1). See my answer explaining the two issues in your code. :) – Dimitre Novatchev Apr 20 '10 at 18:53
  • @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 Apr 21 '10 at 16:28

2 Answers2

5
<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
  • 240,661
  • 26
  • 293
  • 431
  • 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 Apr 21 '10 at 09:51
  • @markusk: This was the code originally provided by the OP -- I just corrected the mistakes. – Dimitre Novatchev Apr 21 '10 at 12:47
  • @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 Apr 21 '10 at 12:50
  • 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 Apr 21 '10 at 14:29
  • @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 `` was replaced with `QQQ`, one would need to be careful with surrounding whitespace, or use `QQQ` instead. – markusk Apr 21 '10 at 15:02
  • @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 Apr 21 '10 at 16:28
  • thanks and I will next time around. For now I think I could address the issue in ZEN, which uses ... Could you recommend some good xsl tutorial, like for dummies? – Elena Apr 21 '10 at 20:19
  • @Elena: See this: http://stackoverflow.com/questions/339930/any-good-xslt-tutorial-book-blog-site-online/341589#341589 – Dimitre Novatchev Apr 21 '10 at 21:36
0

If you want to set the background color of an element, set the "name" of the xsl:attribute to "style" and the value to "background-color: #669933". For example:

<div>
    <xsl:if test="$result  = 0.35 and $column = 0">
        <xsl:attribute name='style'>background-color:#669933</xsl:attribute>
        <xsl:value-of select="$result"/>
    </xsl:if>
    <xsl:if test="$result = 0.10">
        <xsl:attribute name='style'>background-color:#ffffff</xsl:attribute>
        <xsl:value-of select="'QQQ'"/>
    </xsl:if>
</div>
Justin Nafe
  • 3,002
  • 2
  • 18
  • 16