开发者

xsl literal with <xsl:if>

开发者 https://www.devze.com 2022-12-28 01:15 出处:网络
I have to write a simple condition in XSL: IF column=0 AND IF result = .35 set background color to green and write $result

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" /&开发者_如何转开发gt;    

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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消