0

i'm currently facing an issue where i need to increment a variable based on a few checks:

I have the following code with the checks and calculations:

    <xsl:variable name="HBB_Nord_Counter" select="0" />

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

and I have build an if statement where my variable is incremented based on the above mentioned:

<xsl:variable name="HBB_Nord_Counter_2" >
    <xsl:choose>
        <xsl:when test="$HBB_N_Check &gt; 0">
            <xsl:value-of select="$HBB_Nord_Counter + 1" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$HBB_Nord_Counter" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

Now the question:

How can i include the second portion of the code in the first part of the code, so that my variable: HBB_Nord_Counter is incremented by one if the conditions in the second code are met.

I need this variable to divide a sum against it, depending on how many values are greater than 0.

I hope this is understandable enough.

Best regards, Ionut Sanda

  • Possible duplicate of [Increment a value in XSLT](https://stackoverflow.com/questions/3344965/increment-a-value-in-xslt) – Avitus Jan 15 '18 at 13:46
  • As the link in the previous comment explains, variables cannot be incremented in XSLT 1.0. It might be better to reframe your question to explain the problem you are trying to solve, as it might be you don't need to use variables at all. See https://stackoverflow.com/help/mcve. – Tim C Jan 15 '18 at 14:00
  • I have found the question that you are referring to, but it didn't help me. Thanks. – Ionuţ Sanda Jan 15 '18 at 15:12
  • You should tell us what problem you are trying to solve (what the input and output of the transformation are). Saying that you need to do it by incrementing variables isn't helpful, that's like saying you need to do it by squaring the circle. If you tell us what your input and output are, we can show you how to do it without mutable variables. – Michael Kay Jan 15 '18 at 16:05

1 Answers1

0

In other programming languages variables are true variables, i.e. you can:

  • initially assign a value to them,
  • then you can change this value (as many times as you wish).

In XSLT the situation is diffrent: What we call variables are actually constants. You assign a value on creation of a variable and then you cannot change it.

Maybe you should count source elements meeting particular condition? Something like:

<xsl:variable name="cnt" select="count(tagname[condition]"/>

substituting proper tag name and a condition in the predicate.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
  • Thank you for the recommendation, but unfortunately I can't find the exact position, nor the *tagname*, nor the condition i must enter in the above statement so that it works as required. So I guess other than the If I created in the code, there is no other way to increment a variable in xslt? – Ionuţ Sanda Jan 15 '18 at 15:22
  • 1
    Actually, variables in functional programming languages correspond closely to variables in mathematics, whereas variables in procedural programming languages are completely different. So I would quarrel with your use of "true variables" here. – Michael Kay Jan 15 '18 at 16:02
  • I did some research, and found that I can somehow split my file into two pieces, so I did. I have a file which contains all my variables, and one file that contains my whole html part. I did create a file called bmw.xsl which contains all my variables, and at the end of the file I wrote . This main.xsl is the file which has all my html part. When i'm trying to upload this, I do receive an error: "Execute failed: XSLT compile error". – Ionuţ Sanda Jan 16 '18 at 12:30