Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify "not equals to" when comparing strings in an XSLT <xsl:if>?

Tags:

xslt

xpath

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?

like image 412
Maxyie Avatar asked May 10 '12 13:05

Maxyie


People also ask

How do you write not equal to in XSLT?

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.

How do you check not contains in XSLT?

I would probably check for not(starts-with(substring-after(link, '://'), 'www.msdn.com')) , to make the match unambiguous. You could do add something like count(blog[not(contains(following-sibling::link, 'msdn'))]) to get the number of matching blog nodes and use an xsl:choose for the different sections.

How do you write not equal to in XML?

!= path-expression value is not equal to literal . A string or number used to compare against the path-expression node or attribute value. Enclose the string in single or double quotation marks.

How do I write an if statement in XSLT?

The <xsl:if> Element To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document.


1 Answers

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.

like image 174
Michael Kay Avatar answered Nov 11 '22 17:11

Michael Kay