我如何指定“不等于"?在 XSLT <xsl:if> 中比较字符串时? [英] How do I specify &quot;not equals to&quot; when comparing strings in an XSLT &lt;xsl:if&gt;?

查看:36
本文介绍了我如何指定“不等于"?在 XSLT <xsl:if> 中比较字符串时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个带有以下代码的 xsl,只有当它不等于 N/A 时,我才尝试打印出count".但似乎 "!=" 不起作用.

Currently i have a xsl with following code where I'm trying print out "count" only if it is not equal to N/A. but seems like "!=" is not working.

<xsl:for-each select="Directory/Match">
    <xsl:if test = "Count != N/A">
        <tr>
            <td><xsl:value-of select="@bookName" /></td>
            <td><xsl:value-of select="@AuthorName" /></td>
            <td><xsl:value-of select="Count" /></td>
        </tr>
    </xsl:if>
</xsl:for-each>

但是,如果我尝试将其与数值进行比较,它会起作用.

However, it works if I try to compare it with numeric value.

示例:

<xsl:if test = "Occurrances != 0">

谁能告诉我:如果我想比较字符串,我可以使用什么?

Can someone please tell me: If I would like to compare strings what can I use?

推荐答案

正如 Filburt 所说;但也要注意,通常最好写

As Filburt says; but also note that it's usually better to write

test="not(Count = 'N/A')"

如果只有一个 Count 元素,它们的意思是一样的,但如果没有 Count 元素,或者有多个元素,则含义不同.

If there's exactly one Count element they mean the same thing, but if there's no Count, or if there are several, then the meanings are different.

6 年后

既然这个答案似乎很流行,但对某些读者来说可能有点神秘,让我扩展一下.

Since this answer seems to have become popular, but may be a little cryptic to some readers, let me expand it.

XPath 中的="和!="运算符可以比较两组值.一般来说,如果 A 和 B 是一组值,那么如果 A 和 B 中存在任何一对相等的值,则="返回真,而如果有任何一对不相等,则!="返回真.

The "=" and "!=" operator in XPath can compare two sets of values. In general, if A and B are sets of values, then "=" returns true if there is any pair of values from A and B that are equal, while "!=" returns true if there is any pair that are unequal.

在 A 选择零个或一个节点的常见情况下,B 是一个常数(比如NA"),这意味着 not(A = "NA") 返回 true 如果A 要么不存在,要么具有不等于NA"的值.相比之下,如果 A 存在且不等于NA",则 A != "NA" 返回真.通常,您希望将不存在"的情况视为不等于",这意味着 not(A = "NA") 是合适的表述.

In the common case where A selects zero-or-one nodes, and B is a constant (say "NA"), this means that not(A = "NA") returns true if A is either absent, or has a value not equal to "NA". By contrast, A != "NA" returns true if A is present and not equal to "NA". Usually you want the "absent" case to be treated as "not equal", which means that not(A = "NA") is the appropriate formulation.

这篇关于我如何指定“不等于"?在 XSLT <xsl:if> 中比较字符串时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆