Using Numeric Operators to Perform Math Operations in XPath

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Developer's Guide

Using Numeric Operators to Perform Math Operations in XPath

Simple arithmetic operations, such as addition and subtraction, can be used in XPath expressions.

Operator Description/Example
num1 + num2 Adds two numbers, returning their sum.
num1 - num2 Subtracts num2 from num1, returning the difference.
num1 * num2 Multiplies num1 times num2, returning the product.
num1 div num2 Divides num1 by num2, returning the quotient. (This is a floating-point division, which means that the value returned may include a fractional part. 10 div 4 equals 2.5, not 2 or 3.)
num1 mod num2 Returns the modulus—that is, divides num1 by num2, returning the remainder.

Note   Division cannot be performed using a simple slash, /, because that character is used as a separator between the steps in a location path. You must use the div operator to represent division.

+ operator

Add the Northeast and Southeast's sales volumes using an XPath expression such as:

units[../@name="Northeast"] + units[../@name="Southeast"]

- operator

You could calculate the total sales amounts of all regions, except the Northwest, using an XPath expression such as the following:

sum(amount) - amount[../@name="Northwest"]

* operator

To calculate the sales tax on a region's sales amount, multiply the tax rate times the amount. For instance:

amount * .07

(assuming a seven-percent sales tax).

div operator

To calculate and display the average number of units sold across all regions in this quarter, you could use an XSLT template rule such as:

<xsl:template match="sales">
    Average units sold for all regions:
    <xsl:value-of select="sum(.//units) div count(.//units)"/>
</xsl:template>

The result, displayed in Internet Explorer, is:

Average units sold for all regions: 378

mod operator

The mod operator is commonly used for enabling a style sheet's template rules to perform a given action every N times, such as doing one thing on an even-numbered table row and something else on an odd-numbered row.

Example

This example uses the mod operator to display the sales figures for each region in alternating bold and normal text.

XML File (xpathfuncs.xml)

Change the href attribute in xpathfuncs.xml (shown in Sample XML Data File for XPath Functions) to reference mod.xsl.

XSLT File (mod.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="sales">
    <table border="1">
        <tr>
            <th>Region</th>
            <th>Approx Sales</th>
            <th>Units</th>
        </tr>
        <xsl:for-each select="region">
            <xsl:choose>
                <xsl:when test="position() mod 2 = 0">
                    <tr>
                        <td><b>
                            <xsl:value-of select="@name"/>
                        </b></td>
                        <td align="right"><b>
                            <xsl:value-of select="round(amount)"/>
                        </b></td>
                        <td align="right"><b>
                            <xsl:value-of select="units"/>
                        </b></td>
                    </tr>
                </xsl:when>
                <xsl:otherwise>
                    <tr>
                        <td>
                            <xsl:value-of select="@name"/>
                        </td>
                        <td align="right">
                            <xsl:value-of select="round(amount)"/>
                        </td>
                        <td align="right">
                            <xsl:value-of select="units"/>
                        </td>
                    </tr>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>

Formatted Output

Region Approx Sales Units
Northeast 12500 374
Southeast 17692 512
Southwest 8350 161
Northwest 15240 465

Processor Output

<?xml version="1.0" encoding="UTF-16"?><table 
border="1"><tr><th>Region</th><th>Approx 
Sales</th><th>Units</th></tr><tr><td>Northeast</td><td 
align="right">12500</td><td 
align="right">374</td></tr><tr><td><b>Southeast</b></td><td 
align="right"><b>17692</b></td><td 
align="right"><b>512</b></td></tr><tr><td>Southwest</td><td 
align="right">8350</td><td 
align="right">161</td></tr><tr><td><b>Northwest</b></td><td 
align="right"><b>15240</b></td><td 
align="right"><b>465</b></td></tr></table>