[英]XSLT 1.0: Conditional For-Each
我給出了以下XML,其中包含隨機數量的“ Note”元素:
<Notes>
<Note>
<Size>400000</Size>
</Note>
<Note>
<Size>200000</Size>
</Note>
<Note>
<Size>500000</Size>
</Note>
</Notes>
我想檢查一下這些Notes中是否有一個size元素等於或大於500000。如果是這種情況,我想調用主模板。 如果沒有,我想做點別的。
我遇到的問題是:如果我在for-each循環中具有if-then邏輯,那么我將多次調用該模板。 由於xslt中沒有中斷功能,因此我當時想使用一個變量,如果滿足條件,則將其設置為true,如果設置為true,則將for-each稱為模板。 但這不是我真正擔心的最佳方法,您認為呢?
提前致謝。
無需for-each循環。 這樣的事情會做你想要的:
<xsl:template match="Notes">
<xsl:choose>
<xsl:when test="number(descendant::Size/text()) > 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.