74

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.

Example:

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

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

0

2 Answers 2

127

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

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

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 YEARS LATER

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

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.

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.

40

If you want to compare to a string literal you need to put it in (single) quotes:

<xsl:if test="Count != 'N/A'">
1
  • 7
    Perhaps worth adding that the quotation marks are needed because the test makes perfect sense without them: in that case, however, it asks whether the value of a Count child element is not-equal to the value of some A child element of an N child element. Mar 27, 2015 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.