XPath Basics - Operating With Extreme Caution (Page 7 of 9 )
In addition to what you've just seen, XPath also allows you to build expressions using simple logical and comparison operators. Consider the following table, which illustrates the common ones with examples:
Operator What It Means Example
----------------------------------------------------------------------------
--
= is
equal to item[desc = 'Ginger']
!= is not equal to
item[desc != 'Ginger']
is greater than servings > 2
<
is less than servings < 5
= is greater than
or equal to servings >= 2
<= is less than or equal to servings
<= 10
Most of these comparison operators are used in conjunction with XSLT's "if" and "choose" tests (note that if you use them in an XSLT stylesheet, the < symbol must be replaced with the pre-defined XML entity < to avoid XML errors.).
You can combine expressions using the "and" and "or" operators,
Operator Example
----------------------------------------------------------------------------
--
and desc = 'Ginger' and quantity = '1 tsp'
or desc = 'Cinnamon' or servings >= 3
Here's an example of how you could apply this in an XSLT stylesheet:
<xsl:template match="/">
<xsl:if test="//item/desc = 'Cinnamon' or //servings
>= 3">
<xsl:value-of select="//name"/>
</xsl:if>
</xsl:template>
Finally, you can perform arithmetic operations with the various arithmetic operators:
Operator What It Means Example
----------------------------------------------------------------------------
--
+ Addition quantity
+ 5
- Subtraction quantity - 5
* Multiplication quantity * 5
div Division quantity
div 5
mod Modulo quantity mod 5
Here's an example of how you could apply this in an XSLT stylesheet:
<xsl:template match="/">
Current servings: <xsl:value-of select="//servings"/>
Updated
servings: <xsl:value-of select="//servings * 6"/>
</xsl:template>
This article copyright Melonfire 2001. All rights reserved.Next: Be Cool >>
More XML Articles
More By Vikram Vaswani, (c) Melonfire