XPath Operators

Contact Us or call 1-877-932-8228
XPath Operators

XPath Operators

The table below shows the XPath operators.

XPath Operators
Operator Description
and Boolean AND
or Boolean OR
= Equals
!= Not equal
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
+ Addition
- Subtraction
* Multiplication
div Division
mod Modulus

The sample below shows how some operators and functions are used in practice.

Code Sample:

XPath/Demos/BeatlesFunctions.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html"/>
	<xsl:template match="/">
		<h1>Functions and Operators</h1>
		<h2>count()</h2>
		<code>count(beatles/beatle):</code>
		<b>
		<xsl:value-of select="count(beatles/beatle)"/>
		</b>
		<h2>contains()</h2>
		<code>contains(//beatle[last()]/@link,'webucator'):</code>
		<b>
		<xsl:value-of select="contains(//beatle[last()]/@link,'webucator')"/>
		</b><br/>
		<code>contains(//beatle[last()]/@link,'ringostarr'):</code>
		<b>
		<xsl:value-of select="contains(//beatle[last()]/@link,'ringostarr')"/>
		</b>
		<h2>=</h2>
		<code>beatles/beatle[ @real = 'no' ]//firstname:</code>
		<b>
		<xsl:value-of select="beatles/beatle[ @real = 'no' ]//firstname"/>
		</b>
		<h2>!=</h2>
		<code>beatles/beatle[ @real != 'no' ]//firstname:</code>
		<b>
		<xsl:value-of select="beatles/beatle[ @real != 'no' ]//firstname"/>
		</b>
		<h2>not()</h2>
		<code>beatles/beatle[ not(@real) ][2]//firstname:</code>
		<b>
		<xsl:value-of select="beatles/beatle[ not(@real) ][2]//firstname"/>
		</b>
		<h2>last()</h2>
		<code>beatles/beatle[ not(@real) ][last()]//firstname:</code>
		<b>
		<xsl:value-of select="beatles/beatle[ not(@real) ][last()]//firstname"/>
		</b>
		<h2>not() &amp; =</h2>
		<code>beatles/beatle[ not(@real='no') ][2]//firstname:</code>
		<b>
		<xsl:value-of select="beatles/beatle[ not(@real='no') ][2]//firstname"/>
		</b>
		<h2>not() &amp; = &amp; last()</h2>
		<code>beatles/beatle[ not(@real='no') ][last()]//firstname:</code>
		<b>
<xsl:value-of select="beatles/beatle[ not(@real='no') ][last()]//firstname"/>
		</b>
	</xsl:template>
</xsl:stylesheet>

When XPath/Demos/BeatlesFunctions.xml, which has the same XML as XSLTBasics/Demos/Beatles.xml , is transformed against XPath/Demos/BeatlesFunctions.xsl and viewed in a browser, the output looks like this:

Next