如何将XSL变量递增逻辑整合到现有判断代码中?
First, it’s critical to remember that XSLT variables are immutable—once defined, you can’t modify their values later. Instead, we calculate a new variable value based on the original counter and our check condition. Here’s how to cleanly integrate your two code blocks:
Step 1: Combine Check Calculation and Counter Update
We’ll first compute the HBB_N_Check value as you originally did, then derive the updated counter in a concise step (leveraging XSLT’s implicit boolean-to-number conversion: true becomes 1, false becomes 0):
<!-- Initial counter value --> <xsl:variable name="HBB_Nord_Counter" select="0" /> <!-- Calculate the check value as before --> <xsl:variable name="HBB_N_Check"> <xsl:choose> <xsl:when test="($MUC_HBB_Answered_N_WG_1 = '0') or ($MUC_HBB_SL_N_WG_1 = '0') or ($MUC_HBB_Entered_N_WG_1 = '0')">0</xsl:when> <xsl:otherwise> <xsl:value-of select="($MUC_HBB_Answered_N_WG_1 * number(translate($MUC_HBB_SL_N_WG_1, ',', '.'))) div ($MUC_HBB_Entered_N_WG_1)" /> </xsl:otherwise> </xsl:choose> </xsl:variable> <!-- Updated counter: add 1 if HBB_N_Check > 0, else keep original value --> <xsl:variable name="HBB_Nord_Counter_Updated" select="$HBB_Nord_Counter + ($HBB_N_Check > 0)" />
Step 2: Extend for Multiple Checks
Since you mentioned needing this for multiple checks to count how many values are greater than 0, you can scale this pattern by summing the boolean results of each check:
<!-- Example: Add a second check for WG_2 --> <xsl:variable name="HBB_N_Check_WG2"> <xsl:choose> <xsl:when test="($MUC_HBB_Answered_N_WG_2 = '0') or ($MUC_HBB_SL_N_WG_2 = '0') or ($MUC_HBB_Entered_N_WG_2 = '0')">0</xsl:when> <xsl:otherwise> <xsl:value-of select="($MUC_HBB_Answered_N_WG_2 * number(translate($MUC_HBB_SL_N_WG_2, ',', '.'))) div ($MUC_HBB_Entered_N_WG_2)" /> </xsl:otherwise> </xsl:choose> </xsl:variable> <!-- Total counter: sum all checks that return a value > 0 --> <xsl:variable name="Total_HBB_Nord_Counter" select="0 + ($HBB_N_Check > 0) + ($HBB_N_Check_WG2 > 0)" />
Step 3: Use the Counter for Division (Avoid Division by Zero)
When using the counter to divide a total sum, always add a safety check to prevent division by zero errors:
<!-- Calculate total sum of valid check values --> <xsl:variable name="Total_Sum" select="$HBB_N_Check + $HBB_N_Check_WG2" /> <!-- Compute final result with division safety --> <xsl:variable name="Final_Result"> <xsl:choose> <xsl:when test="$Total_HBB_Nord_Counter = 0">0</xsl:when> <xsl:otherwise> <xsl:value-of select="$Total_Sum div $Total_HBB_Nord_Counter" /> </xsl:otherwise> </xsl:choose> </xsl:variable>
Key Takeaways
- The boolean-to-number conversion trick lets us avoid verbose
<xsl:choose>blocks for counter increments—any condition evaluating totruebecomes1in a numeric context, andfalsebecomes0. - Since XSLT is a functional language, we always create new variables instead of modifying existing ones. This is the core pattern for any "state-like" behavior in XSLT.
内容的提问来源于stack exchange,提问作者Ionuţ Sanda

