简体   繁体   中英

XSLT 1.0: Conditional For-Each

I have the following XML given, with a random amount of "Note" elements:

<Notes>
   <Note>
       <Size>400000</Size>
   </Note>
   <Note>
       <Size>200000</Size>
   </Note>
   <Note>
       <Size>500000</Size>
   </Note>
</Notes>

I want to check, if one of these Notes have a size element equal or greater to 500000. If that's the case, I want to call the main template. If not, I want to do something else.

The issue I have is: If I have the if-then logic inside the for-each loop, then I would call the template multiple times. As there is no break-functionality in xslt, I was then thinking to use a variable which I would set to true if the condition is met, and afther the for-each I'd call the template if it was set to true. But this isn't the best approach really I fear, what do you think?

Thanks in advance.

No need for a for-each loop. Something like this will do what you want:

<xsl:template match="Notes">
    <xsl:choose>
        <xsl:when test="number(descendant::Size/text()) &gt; 500000">
        <xsl:call-template name="process Note"/>
        </xsl:when>
        <xsl:otherwise>
        <xsl:call-template name="add attribute to Note"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM