Is there any way to exit for each loop immediately once condition gets true?

Nikhil Karande

i need to exit for each loop immediately once condition gets true. and want to return current index where loop breaks. below is my sample xml and xslt

<xsl:stylesheet xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
    <xsl:template match="Data">
        <xsl:element name="Data">
            <xsl:call-template name="TempData" />
        </xsl:element>
    </xsl:template>
    <xsl:template name="TempData">
        <xsl:element name="TempData">
            <xsl:for-each select="/Data/Subject/SubTest">
                    <xsl:choose>
                        <xsl:when test="@System='OK' and @SubFlag!='1'">
                            <xsl:attribute name="CurrentSubFlag">
                                <xsl:value-of select="position()" />
                            </xsl:attribute>
                        </xsl:when>
                    </xsl:choose>
                </xsl:for-each>
            <xsl:copy-of select="TempData/*" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

<Data>
    <Subject>
        <SubTest SubFlag="1" System="OK"> </SubTest>
        <SubTest SubFlag="csdcd" System="OK"> </SubTest>
        <SubTest SubFlag="zxczx" System="OK"> </SubTest>  
    </Subject>
    <TempData CurrentSubFlag="abc"/>
</Data>

output xml should be

<Data>
    <Subject>
        <SubTest SubFlag="1" System="OK"> </SubTest>
        <SubTest SubFlag="csdcd" System="OK"> </SubTest>
        <SubTest SubFlag="zxczx" System="OK"> </SubTest>  
    </Subject>
    <TempData CurrentSubFlag="2"/>
</Data>

if input is

<Data>
    <Subject>
        <SubTest SubFlag="1" System="OK"> </SubTest>
        <SubTest SubFlag="1" System="OK"> </SubTest>
        <SubTest SubFlag="1" System="OK"> </SubTest>  
    </Subject>
    <TempData CurrentSubFlag="abc"/>
</Data>

then output will be same CurrentSubFlag="abc"

michael.hor257k

xsl:for-each is not a loop, and the result you need can be accomplished without a loop or recursion.

Given:

XML

<Data>
    <Subject>
        <SubTest SubFlag="1" System="OK"> </SubTest>
        <SubTest SubFlag="csdcd" System="OK"> </SubTest>
        <SubTest SubFlag="zxczx" System="OK"> </SubTest>  
    </Subject>
    <TempData CurrentSubFlag="abc"/>
</Data>

the following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="TempData">
    <xsl:variable name="index" select="count(/Data/Subject/SubTest[not(@SubFlag=1 and @System='OK')][1]/preceding-sibling::SubTest) + 1" />
    <TempData CurrentSubFlag="{$index}"/>
</xsl:template>

</xsl:stylesheet>

will return:

Result

<?xml version="1.0" encoding="UTF-8"?>
<Data>
  <Subject>
    <SubTest SubFlag="1" System="OK"/>
    <SubTest SubFlag="csdcd" System="OK"/>
    <SubTest SubFlag="zxczx" System="OK"/>
  </Subject>
  <TempData CurrentSubFlag="2"/>
</Data>

Added:

To accommodate your added condition, try:

<xsl:template match="TempData">
    <xsl:variable name="fail" select="/Data/Subject/SubTest[not(@SubFlag=1 and @System='OK')]" />
    <xsl:choose>
        <xsl:when test="$fail">
            <TempData CurrentSubFlag="{count($fail[1]/preceding-sibling::SubTest) + 1}"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy-of select="."/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

foreach loop isn't continuing once an `IF/ELSEIF` statement is true

분류에서Dev

JPA Join Column with where condition OR filter an entity with any declarative way

분류에서Dev

How to exit a while loop

분류에서Dev

If either or both condition true

분류에서Dev

Factory in $interval only gets called once?

분류에서Dev

Compile regex only once and cleanup on program exit

분류에서Dev

and () and any () with custom condition

분류에서Dev

Why doesn't sed exit immediately after writing the output?

분류에서Dev

Any Ideas how to iterate an element in the each loop in jQuery from within a click function within it?

분류에서Dev

sh test two condition gets [: missing`] '

분류에서Dev

How to replace each word once

분류에서Dev

Is there any way to loop trough these numbers: -1 0, 1 0, 0 -1, 0 1

분류에서Dev

Is there any way of writing a section of HTML so that a CSS selector would produce an infinite loop?

분류에서Dev

Why is this PHP if condition always true?

분류에서Dev

Is there a way to create a loop logic that can select different option for each dropdown list and repeat it again for the next option?

분류에서Dev

While loop if condition is not met

분류에서Dev

Break condition to continue loop

분류에서Dev

condition check in for loop in C

분류에서Dev

Using a condition in a while loop

분류에서Dev

'and' condition in for loop in django template

분류에서Dev

Exit bash when find gets to a folder with permission denied

분류에서Dev

DownloadFileAsync in loop with CompletedEvent gets only the last file

분류에서Dev

Are there any official ways to write an Immediately Invoked Function Expression?

분류에서Dev

MySQL max value for each condition

분류에서Dev

#each loop {{this }} not getting populated

분류에서Dev

for each loop listview

분류에서Dev

Each loop in Ruby on Rails

분류에서Dev

Is there a way to start over in a For Each?

분류에서Dev

Program will not exit a do-while loop

Related 관련 기사

  1. 1

    foreach loop isn't continuing once an `IF/ELSEIF` statement is true

  2. 2

    JPA Join Column with where condition OR filter an entity with any declarative way

  3. 3

    How to exit a while loop

  4. 4

    If either or both condition true

  5. 5

    Factory in $interval only gets called once?

  6. 6

    Compile regex only once and cleanup on program exit

  7. 7

    and () and any () with custom condition

  8. 8

    Why doesn't sed exit immediately after writing the output?

  9. 9

    Any Ideas how to iterate an element in the each loop in jQuery from within a click function within it?

  10. 10

    sh test two condition gets [: missing`] '

  11. 11

    How to replace each word once

  12. 12

    Is there any way to loop trough these numbers: -1 0, 1 0, 0 -1, 0 1

  13. 13

    Is there any way of writing a section of HTML so that a CSS selector would produce an infinite loop?

  14. 14

    Why is this PHP if condition always true?

  15. 15

    Is there a way to create a loop logic that can select different option for each dropdown list and repeat it again for the next option?

  16. 16

    While loop if condition is not met

  17. 17

    Break condition to continue loop

  18. 18

    condition check in for loop in C

  19. 19

    Using a condition in a while loop

  20. 20

    'and' condition in for loop in django template

  21. 21

    Exit bash when find gets to a folder with permission denied

  22. 22

    DownloadFileAsync in loop with CompletedEvent gets only the last file

  23. 23

    Are there any official ways to write an Immediately Invoked Function Expression?

  24. 24

    MySQL max value for each condition

  25. 25

    #each loop {{this }} not getting populated

  26. 26

    for each loop listview

  27. 27

    Each loop in Ruby on Rails

  28. 28

    Is there a way to start over in a For Each?

  29. 29

    Program will not exit a do-while loop

뜨겁다태그

보관