如果满足条件,如何在XSL中退出“ for-each”循环?
例如假设我希望XSL在以下XML中显示以下XSL代码,其中包含(两间卧室且租金

<xsl:for-each select="//apartment/apartment_details">
  <xsl:if test="bedrooms=$bedrooms and rent &lt;= $budget ">
    <!--display apartment address-->
  </xsl:if>
</xsl:for-each>


将两次返回相同的公寓地址。即使有多个满足条件的公寓,我也只希望显示一次公寓地址。

XML结构:

<apartments>
  <apartment>
    <address>
        <street>....</street>
        <city>....</city>
    </address>
    <apartment_details>
        <bedrooms>2</bedrooms>
        <bathrooms>2</bathrooms>
        <rent>1000</rent>
    </apartment_details>
    <apartment_details>
        <bedrooms>2</bedrooms>
        <bathrooms>1</bathrooms>
        <rent>900</rent>
    </apartment_details>
    ...
  </apartment>
  ...
</apartments>


谢谢。

最佳答案

如果满足条件,如何在XSL中退出“ for-each”循环?


这不可能。没有任何XSLT指令可退出xsl:for-each的处理并继续执行转换。您可以做的是精确指定所选节点应满足的条件。

用:

<xsl:for-each select=
 "/*/apartment
      [apartment_details[bedrooms=$bedrooms and $budget  >= rent]]">
  <!-- output apartment address here -->
</xsl:for-each>


此代码显示任何apartment的地址,该apartment_details是XML文档顶部元素的子元素,并且具有bedrooms子元素,其子元素renttrue()bedrooms=$bedrooms and $budget >= rent

关于xslt - XSLT:如果满足条件,如何退出“for-each”循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10392614/

10-10 14:04