简体   繁体   English

XSLT 1.0:有条件的

[英]XSLT 1.0: Conditional For-Each

I have the following XML given, with a random amount of "Note" elements: 我给出了以下XML,其中包含随机数量的“ Note”元素:

<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. 我想检查一下这些Notes中是否有一个size元素等于或大于500000。如果是这种情况,我想调用主模板。 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. 我遇到的问题是:如果我在for-each循环中具有if-then逻辑,那么我将多次调用该模板。 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. 由于xslt中没有中断功能,因此我当时想使用一个变量,如果满足条件,则将其设置为true,如果设置为true,则将for-each称为模板。 But this isn't the best approach really I fear, what do you think? 但这不是我真正担心的最佳方法,您认为呢?

Thanks in advance. 提前致谢。

No need for a for-each loop. 无需for-each循环。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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