Extensions to XSLT 1.0 (EXSLT 1.0) - Math

This version: exslt-math-010302.html
Most Recent version: http://www.jenitennison.com/xslt/exslt/math/
Author: Jeni Tennison

Abstract

This document describes EXSLT 1.0 - Math. EXSLT 1.0 is a set of extension elements and functions that XSLT authors may find helpful when creating stylesheets. EXSLT 1.0 - Math covers those extension elements and functions that provide facilities to do with maths.

Other parts of EXSLT 1.0 include:

Status of this Document

This document is a first draft for review by the implementers of XSLT processors and the XSLT stylesheet authors. It is based on discussions on XSL-List. Comments on this document should be sent to XSL-List.

This document has no official standing and has not been considered nor approved by any organization.

Contents

1. Introduction
2. Namespace
3. Math Functions

Appendices

A. References
B. Implementations in EXSLT
C. Acknowledgements

1. Introduction

This document describes EXSLT 1.0 - Math. EXSLT 1.0 is a set of extension elements and functions that XSLT authors may find helpful when creating stylesheets. EXSLT 1.0 - Math covers those extension elements and functions that provide facilities to do with maths.

The extension elements and functions defined within this document are governed by the general rules about extensions to XSLT covered in [14. Extensions] in [XSLT 1.0].

An XSLT processor that supports EXSLT 1.0 - Math using the extension elements and functions defined within this document must conform to the behaviour described within this document. An XSLT processor that supports EXSLT 1.0 - Math is not required to support any other parts of EXSLT 1.0.

Issue: EXSLT 1.0 - Common support - should a processor that supports EXSLT 1.0 - Math be required to support EXSLT 1.0 - Common?

2. Namespace

The namespace for the extension elements and functions described in this document is:

http://xmlns.opentechnology.org/xslt-extensions/math
      

Throughout this document, the prefix num is used to refer to this namespace. Any other prefix can be used within a particular stylesheet (though a prefix must be specified to enable the extension functions to be recognised as extensions).

3. Math Functions

This section defines the extension functions in EXSLT 1.0 - Math.

Issue: EXSLT 1.0 - Math Functions - some of these functions have string arguments that are dynamically evaluated as expressions. Might it be best to ignore these functions for EXSLT 1.0 and address them when we generally address dynamic evaluation?

Function: number num:max(node-set, string?)

The num:max function returns the maximum value of the nodes in the node set passed as the first argument. The 'value' of a node is calculated by evaluating the expression held in the string passed as the second argument with the current node equal to the node whose value is being calculated.

Function: number num:min(node-set, string?)

The num:min function returns the minimum value of the nodes in the node set passed as the first argument. The 'value' of a node is calculated by evaluating the expression held in the string passed as the second argument with the current node equal to the node whose value is being calculated.

Function: node-set num:highest(node-set, string?)

The num:highest function returns the nodes in the node set passed as the first argument with the highest value. The 'value' of a node is calculated by evaluating the expression held in the string passed as the second argument with the current node equal to the node whose value is being calculated.

Function: node-set num:lowest(node-set, string?)

The num:lowest function returns the nodes in the node set passed as the first argument with the lowest value. The 'value' of a node is calculated by evaluating the expression held in the string passed as the second argument with the current node equal to the node whose value is being calculated.

Function: number num:sum(node-set, string?)

The num:sum function returns the sum of the values of the nodes in the node set passed as the first argument. The 'value' of a node is calculated by evaluating the expression held in the string passed as the second argument with the current node equal to the node whose value is being calculated.

A. References

EXSLT Common
Jeni Tennison. Extensions to XSLT 1.0 (EXSLT 1.0) - Common. See http://www.jenitennison.com/xslt/exslt/common
XSLT
World Wide Web Consortium. XSL Transformations (XSLT). W3C Recommendation. See http://www.w3.org/TR/xslt
XPath
World Wide Web Consortium. XML Path Language. W3C Recommendation. See http://www.w3.org/TR/xpath

B. Sample Extension Functions

This appendix holds example implementations of the functions defined in this document, using exsl:function as described in [EXSLT Common].

Function: number num:max(node-set, string?)

Note that the use of the second argument is fudged in this implementation: the 'value' of a node is calculated using com:eval with a first argument being the node, and the second argument being the string passed as the second argument to num:max.

<exsl:function name="num:max">
   <xsl:param name="node-set" select="/.." />
   <xsl:param name="expr" select="'.'" />
   <xsl:variable name="value-of-first" 
                 select="number(com:eval($node-set[1], $expr))" />
   <xsl:choose>
      <xsl:when test="count($node-set) <= 1">
         <exsl:result select="$value-of-first" />
      </xsl:when>
      <xsl:otherwise>
         <xsl:variable name="max-of-rest"
                          select="num:max($node-set[position() != 1], $expr)" />
         <xsl:choose>
            <xsl:when test="$value-of-first > $max-of-rest">
               <exsl:result select="$value-of-first" />
            </xsl:when>
            <xsl:otherwise>
               <exsl:result select="$max-of-rest" />
            </xsl:otherwise>
         </xsl:choose>
      </xsl:otherwise>
   </xsl:choose>
</exsl:function>
      

Function: number num:min(node-set, string?)

Note that the use of the second argument is fudged in this implementation: the 'value' of a node is calculated using com:eval with a first argument being the node, and the second argument being the string passed as the second argument to num:min.

<exsl:function name="num:min">
   <xsl:param name="node-set" select="/.." />
   <xsl:param name="expr" select="'.'" />
   <xsl:variable name="value-of-first" 
                 select="number(com:eval($node-set[1], $expr))" />
   <xsl:choose>
      <xsl:when test="count($node-set) <= 1">
         <exsl:result select="$value-of-first" />
      </xsl:when>
      <xsl:otherwise>
         <xsl:variable name="min-of-rest"
                          select="num:min($node-set[position() != 1], $expr)" />
         <xsl:choose>
            <xsl:when test="$value-of-first < $max-of-rest">
               <exsl:result select="$value-of-first" />
            </xsl:when>
            <xsl:otherwise>
               <exsl:result select="$min-of-rest" />
            </xsl:otherwise>
         </xsl:choose>
      </xsl:otherwise>
   </xsl:choose>
</exsl:function>
      

Function: node-set num:highest(node-set, string?)

Note that the use of the second argument is fudged in this implementation: the 'value' of a node is calculated using com:eval with a first argument being the node, and the second argument being the string passed as the second argument to num:highest.

<exsl:function name="num:highest">
   <xsl:param name="node-set" select="/.." />
   <xsl:param name="expr" select="'.'" />
   <xsl:choose>
      <xsl:when test="count($node-set) <= 1">
         <exsl:result select="$node-set[1]" />
      </xsl:when>
      <xsl:otherwise>
         <xsl:variable name="highest-of-rest"
                          select="num:highest($node-set[position() != 1], 
                                              $expr)" />
         <xsl:variable name="highest-of-rest-value"
                          select="number(com:eval($highest-of-rest, $expr))" />
         <xsl:variable name="current-value"
                          select="number(com:eval($node-set[1], $expr))" />
         <xsl:choose>
            <xsl:when test="$current-value = $highest-of-rest-value">
               <exsl:result select="$node-set[1] | $highest-of-rest" />
            </xsl:when>
            <xsl:when test="$current-value > $highest-of-rest-value">
               <exsl:result select="$node-set[1]" />
            </xsl:when>
            <xsl:otherwise>
               <exsl:result select="$highest-of-rest" />
            </xsl:otherwise>
         </xsl:choose>
      </xsl:otherwise>
   </xsl:choose>
</exsl:function>
      

Function: node-set num:lowest(node-set, string?)

Note that the use of the second argument is fudged in this implementation: the 'value' of a node is calculated using com:eval with a first argument being the node, and the second argument being the string passed as the second argument to num:lowest.

<exsl:function name="num:lowest">
   <xsl:param name="node-set" select="/.." />
   <xsl:param name="expr" select="'.'" />
   <xsl:choose>
      <xsl:when test="count($node-set) <= 1">
         <exsl:result select="$node-set[1]" />
      </xsl:when>
      <xsl:otherwise>
         <xsl:variable name="lowest-of-rest"
                          select="num:lowest($node-set[position() != 1], 
                                             $expr)" />
         <xsl:variable name="lowest-of-rest-value"
                          select="number(com:eval($lowest-of-rest, $expr))" />
         <xsl:variable name="current-value"
                          select="number(com:eval($node-set[1], $expr))" />
         <xsl:choose>
            <xsl:when test="$current-value = $lowest-of-rest-value">
               <exsl:result select="$node-set[1] | $lowest-of-rest" />
            </xsl:when>
            <xsl:when test="$current-value < $lowest-of-rest-value">
               <exsl:result select="$node-set[1]" />
            </xsl:when>
            <xsl:otherwise>
               <exsl:result select="$highest-of-rest" />
            </xsl:otherwise>
         </xsl:choose>
      </xsl:otherwise>
   </xsl:choose>
</exsl:function>
      

Function: number num:sum(node-set, string?)

Note that the use of the second argument is fudged in this implementation: the 'value' of a node is calculated using com:eval with a first argument being the node, and the second argument being the string passed as the second argument to num:sum.

<exsl:function name="num:sum">
   <xsl:param name="node-set" select="/.." />
   <xsl:param name="expr" select="'.'" />
   <xsl:variable name="value-of-first"
                    select="number(com:eval($node-set[1], $expr))" />
   <xsl:choose>
      <xsl:when test="count($node-set) <= 1">
         <exsl:result select="$value-of-first" />
      </xsl:when>
      <xsl:otherwise>
         <exsl:result select="$value-of-first +
                                 num:sum($node-set[position() != 1], $expr)" />
      </xsl:otherwise>
   </xsl:choose>
</exsl:function>
      

C. Acknowledgements

This document describes many functions that are currently in use in Saxon by Mike Kay. It has also been informed and inspired by discussions on XSL-List.