简体   繁体   中英

Why is xsl:when and xsl:value-of not working here?

A choose - when - test construct in XSLT is not behaving the way I think it should. Actually, it doesn't appear to be doing anything.

The environment is Workday Studio. I'm making a change to a program that has been running every night for months. The constructs in this code are very similar to other parts of the code, which is partly why this is baffling.

<xsl:variable name="CC_Fee_Calc">
    <xsl:value-of select="number($value_from_salesforce) * number($CreditCard_RecoveryFee_Percent) * 0.01"/>
</xsl:variable>
<xsl:variable name="CC_Fee_IsZero">
    <xsl:choose>
        <xsl:when test="number($CC_Fee_Calc) &lt; 0.0">
            <xsl:value-of select="Y"/> 
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="N"/> 
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable> 

<CC_Fee_Calc><xsl:value-of select="$CC_Fee_Calc"/></CC_Fee_Calc>
<CC_Fee_Zero><xsl:value-of select="$CC_Fee_IsZero"/></CC_Fee_Zero>

Note that the HTML version of the less-than sign < (in line 6) is necessary due to the XML nature of the XSLT. The same thing works fine elsewhere in the code.

The last two lines exist only for displaying the contents of the variables. They create this output in the log (for differing input data):

<CC_Fee_Calc>150.432</CC_Fee_Calc>
<CC_Fee_Zero/>

<CC_Fee_Calc>-0.632</CC_Fee_Calc>
<CC_Fee_Zero/>

<CC_Fee_Calc>-1</CC_Fee_Calc>
<CC_Fee_Zero/>

The values in the CC_Fee_Calc field are correct. But for CC_Fee_Zero , it is as if the variable is merely getting defined, and not populated. I'm expecting N for the first example, and Y for the other two.

Change

        <xsl:value-of select="Y"/> 

to

        <xsl:value-of select="'Y'"/> 

Otherwise you're specifying an element named Y (which is not found) rather than a "Y" string.

You'll want to make a similar change for N .


Note : In the context of the xsl:when and xsl:otherwise , you can go further and just drop xsl:value-of altogether:

    <xsl:when test="number($CC_Fee_Calc) &lt; 0.0">Y</xsl:when>
    <xsl:otherwise>N</xsl:otherwise>

Credit: Thanks to @michael.hor257k for the suggestion for this simplification.

Another useful simplification to the code is to replace

<xsl:variable name="CC_Fee_Calc">
    <xsl:value-of select="number($value_from_salesforce) * number($CreditCard_RecoveryFee_Percent) * 0.01"/>
</xsl:variable>

by

<xsl:variable name="CC_Fee_Calc" 
  select="number($value_from_salesforce) * number($CreditCard_RecoveryFee_Percent) * 0.01"/>

The second version computes a number and binds it to the variable. Your version computes a number, converts it to a string, creates a text node containing this string, creates a document node as the parent of this text node, and binds it to the variable. Then when you do number($CC_Fee_Calc) it has to find the string value of the document node, which means navigating to the text node, extract the string from the text node, and convert it back to a number. All so that you can write three lines of code instead of one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM