【问题标题】:xsl:if test = there are NO matching elements?xsl:if test = 没有匹配的元素?
【发布时间】:2013-10-23 04:39:08
【问题描述】:

给定这样的 XML

<data>
    <item temp="cool"/>
    <item temp="hot"/>
    <item temp="hot"/>
    <item temp="lukewarm"/>
</data>

我可以很容易地测试是否有任何东西是热的:

<xsl:if test="/data/item/@temp = 'hot'">
    Something's hot in here
</xsl:if>

但是测试是否没有任何东西是热的对我来说有点令人费解:

<xsl:choose>
    <xsl:when test="/data/item/@temp = 'hot'"></xsl:when>
    <xsl:otherwise>
        Nothing's hot in here.
    </xsl:otherwise>
</xsl:choose>

我有几种情况,我只想在非情况下附加一些元素,所以我想消除额外的代码。

无论如何我可以把它写成一个测试语句吗?

顺便说一句,这不起作用

<xsl:if test="/data/item/@temp != 'hot'">
    Nothing's hot in here
</xsl:if>

因为只要一件事不热,它就会过去。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    试试这个作为 XPath:not(/data/item[@temp='hot'])

    这转换为where there does not exist an &lt;item&gt; that has a "temp" attribute with value "hot"

    【讨论】:

    • != 用于比较值。 not() 用于节点集。
    猜你喜欢
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 2013-10-06
    • 2021-12-19
    • 2019-05-03
    相关资源
    最近更新 更多