Specifying Number Operations and Functions

This section includes the following topics:

Performing Arithmetic Operations

In queries, a number represents a floating-point number. A number can have any double-precision 64-bit format IEEE 754 value. This includes

  • A special not-a-number (NaN) value
  • Positive and negative infinity
  • Positive and negative zero

The numeric operators convert their operands to numbers as if by calling the number() function. See Converting an Object to a Number.

You can use the following arithmetic operators in queries:

  • + performs addition
  • - performs subtraction
  • XML allows hyphens ( -) in names. Consequently, the subtraction operator ( -) typically needs to be preceded by white space. For example, foo-bar evaluates to a node set that contains the child elements named foo-bar. However, foo - bar evaluates to the difference between the result of converting the string value of the first foo child element to a number and the result of converting the string value of the first bar child to a number.

  • * performs multiplication
  • mod returns the remainder from a truncating division. For example:
    • 5 mod 2 returns 1.
    • 5 mod -2 returns 1.
    • -5 mod 2 returns -1.
    • -5 mod -2 returns -1.
    • The mod operator is the same as the % operator in Java and ECMAScript. But it does not perform the same operation as the IEEE remainder operation, which returns the remainder from a rounding division.

  • div performs floating-point division according to IEEE 754.

Calling Number Functions

This section describes the number functions that you can call in a query. The operations you can perform are

Converting an Object to a Number

In some situations, you might want to force a numeric comparison. The XPath processor performs a numeric comparison if either operand is numeric and neither is Boolean. (If one operand is Boolean, the XPath processor converts the other to Boolean and performs a Boolean comparison.) However, if neither operand is a numeric or Boolean value, you can call the number() function on one operand to convert it to a numeric value. The XPath processor automatically converts the other operand to a numeric value.

To perform a numeric comparison, you must call the number() function to convert a Boolean operand, if there is one, to a numeric value.

The format of the number() function is

number number(object?) 
               

            

If you omit the argument, the value of the argument defaults to a node set with the context node as its only member. Table 62 shows how the number() function converts its argument to a number:

Argument
Converted Value
String that consists of optional white space followed by an optional minus sign followed by a number followed by white space
IEEE 754 number that is nearest to the mathematical value represented by the string.
Any other string
NaN (not a number)
Boolean true
1
Boolean false
0
Node set
First the XPath processor converts the node set to a concatenated string as if by a call to the string() function for the first node in the node set, in document order. The XPath processor then converts this string the same way as it would a string argument.
Table 62. number() Function Arguments and Converted Values

Obtaining the Sum of the Values in a Node Set

To obtain the sum of the values of the nodes in a set, call the sum() function. The format is

number sum(node-set
              ) 
               

            

For each node in the argument node-set, the XPath processor converts the string value of the node to a number. The sum() function returns the sum of these numbers.

Obtaining the Largest, Smallest, or Closest Number

To obtain the largest integer that is not greater than a particular number, call the floor() function. The format is

number floor(number) 
               

            

The floor() function returns the largest (closest to positive infinity) number that is not greater than the argument and that is an integer. For example:

  • floor(5.3) returns 5
  • floor(-5.3) returns -6

To obtain the smallest integer that is not less than a particular number, call the ceiling() function. The format is

number ceiling(number) 
               

            

The ceiling() function returns the smallest (closest to negative infinity) number that is not less than the argument and that is an integer. For example:

  • ceiling(5.3) returns 6
  • ceiling(-5.3) returns -5

To obtain the closest integer to a particular number, call the round() function. The format is

number round(number) 
               

            

The round() function returns the number that is closest to the argument and that is an integer. If there are two such numbers, the function returns the one that is closest to positive infinity. For example:

  • round(5.3) returns 5
  • round(5.6) returns 6
  • round(5.5) returns 6

EXSLT Tools

EXSLT support in Stylus Studio lets you extend XSLT 1.0 stylesheets and simplify many tasks including computing dates and times, performing mathematical computations, string manipulations and more.

Web Service Data Mapping

Stylus Studio makes it possible for you to use Web services as live XML data sources for XML mapping projects.

XML Project

Stylus Studio's Project Window is an intuitive, visual feature for organizing files belonging to a project. It supports different file-type icons and nested folders; many helpful options are available from the context menu.

Learn XQuery in 10 Minutes, by Dr. Michael Kay - An XQuery Tutorial

Learn XQuery in 10 Minutes By Dr. Michael Kay is for all those people who really want to know what XQuery is, but don't have the time to find out. It's the fastest XQuery tutorial on the planet - read it now!

Stylus Most Wanted

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.