开发者

how to do a continue in an xslt for-each

开发者 https://www.devze.com 2023-03-19 07:59 出处:网络
How to do a continue in an xslt for-each (not exiting the for-each, but rather continue the for-each?

How to do a continue in an xslt for-each (not exiting the for-each, but rather continue the for-each?

Like:

<xsl:for-each sele开发者_C百科ct="asd">

    <xsl:if test="$test1">
        <!--some stuff-->
        <xsl:if test="$test1A">
            <!--CONTINUE()-->
        </xsl:if>
    </xsl:if>

    <xsl:if test="$test2">
        <!--some stuff-->
        <!--CONTINUE()-->
    </xsl:if>

    <!--main stuff-->
</xsl:for-each>


In this specific case, seems you want possibly execute both codes according to a condition. In fact you want to continue from the first if only if $test1A is true.

In this case xsl:choose does not help you. You have to work with pure logic and emulate the wanted behavior:

<xsl:for-each select="asd">
    <xsl:if test="$test1">
        <!--some stuff-->
        <xsl:if test="$test1A">
            <!--CONTINUE()-->
        </xsl:if>
    </xsl:if>
    <xsl:if test="$test2 and not($test1A)">
        <!--some stuff-->
        <!--CONTINUE()-->
    </xsl:if>
    <!--main stuff-->
</xsl:for-each>

Use conditions as above, you will execute the second if only if the nested if in the first branch is false.


Think you need xml:choose and xml:when. xsl:choose element selects one among a number of possible alternatives. So when expression is evaluate to true, it execute that block and then goes to the next loop.


There is no construct analogous to a continue in XSL. You will have to restructure your code flow to achieve the same effect.

I would suggest breaking up the entirety of the loop body into smaller chunks in order to minimize the number of compound if's.

Edit:

Here's what it looks like when you use full compound ifs:

<xsl:for-each select="asd">
    <xsl:if test="$test1">
        <!--some stuff (1)-->
    </xsl:if>
    <xsl:if test="not($test1 and $test1A)">
        <xsl:choose>
            <xsl:when test="$test2">
                <!--some stuff (2)-->            
            </xsl:when>
            <xsl:otherwise>
                <!--main stuff (3)-->    
            </xsl:otherwise>
        </xsl:choose>
    </xsl:if>
</xsl:for-each>

Every time you need a continue you need to wrap all following code in a guard.

The code is harder to read as a single statement and if there is any substantial amount of code inside those if's you would do well to break them out into their own templates and call out to them.

0

精彩评论

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