user2111362
user2111362

Reputation: 1

xsl variable value modification

<xsl:variable name="sessionvalue">+xyz+</xsl:variable>
        <xsl:variable name="controller">1</xsl:variable>
        <xsl:for-each select="/Properties/Data/Datum[@Name='My DCR']/DCR/tip_of_the_week/targeted_content">
            <xsl:variable name="dcrvalue"><xsl:value-of select="targeted_audiences"/></xsl:variable>
            <span>controller: <xsl:value-of select="$controller"/></span>
            <xsl:if test="$controller='1'">
            <xsl:if test="contains($dcrvalue,$sessionvalue)">
            <xsl:variable name="controller">0</xsl:variable>
                <p><xsl:value-of select="tip_header"/></p>
            </xsl:if>
            </xsl:if>
         </xsl:for-each>

I need to come out of the for each loop on one successful contains test. I see that there is break available in xslt neither can we values of a variable. Does anyone have any suggestion for this?

Upvotes: 0

Views: 86

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117140

I need to come out of the for each loop on one successful contains test.

No. What you need to do is test if at least one of the nodes satisfies the condition. No "loop" is necessary for this.

I put "loop" in quotes, because xsl:for-each is not a loop.

I cannot read your code, but in general it would look something like this:

<xsl:choose>
    <xsl:when test="some/nodes[contains(sub-node, 'search-string')]">
        <!-- do something -->
    </xsl:when>
    <xsl:otherwise>
        <!-- do something else -->
    </xsl:otherwise>
</xsl:choose> 

Edit

It can happen that there maybe multiple matches but i need to work on the very first match.

If that's your goal, then select the very first match directly, for example:

<xsl:for-each select="some/nodes[contains(sub-node, 'search-string')][1]">
    <!-- work on this node -->
</xsl:for-each>

Again, no "loop" is necessary for this.

Upvotes: 1

Related Questions