XPath Operators

Welcome to our free XML tutorial. This tutorial is based on Webucator's Introduction to XML Training course.

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:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
	<xsl:output method="html"/>
	<xsl:template match="/">
		<h1>Functions and Operators</h1>
		<xsl:value-of select="count(beatles/beatle)"/>
		<xsl:value-of select="contains(//beatle[last()]/@link,'webucator')"/>
		<xsl:value-of select="contains(//beatle[last()]/@link,'ringostarr')"/>
		<code>beatles/beatle[ @real = 'no' ]//firstname:</code>
		<xsl:value-of select="beatles/beatle[ @real = 'no' ]//firstname"/>
		<code>beatles/beatle[ @real != 'no' ]//firstname:</code>
		<xsl:value-of select="beatles/beatle[ @real != 'no' ]//firstname"/>
		<code>beatles/beatle[ not(@real) ][2]//firstname:</code>
		<xsl:value-of select="beatles/beatle[ not(@real) ][2]//firstname"/>
		<code>beatles/beatle[ not(@real) ][last()]//firstname:</code>
		<xsl:value-of select="beatles/beatle[ not(@real) ][last()]//firstname"/>
		<h2>not() &amp; =</h2>
		<code>beatles/beatle[ not(@real='no') ][2]//firstname:</code>
		<xsl:value-of select="beatles/beatle[ not(@real='no') ][2]//firstname"/>
		<h2>not() &amp; = &amp; last()</h2>
		<code>beatles/beatle[ not(@real='no') ][last()]//firstname:</code>
<xsl:value-of select="beatles/beatle[ not(@real='no') ][last()]//firstname"/>

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:Output


This tutorial is based on Webucator's Introduction to XML Training Course. We also offer many other XML Training courses. Sign up today to get help from a live instructor.