Extensions to XSLT 1.0 (EXSLT 1.0) - Sets

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

Abstract

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

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. Set Functions

Appendices

A. References
B. Implementations in EXSLT
C. Acknowledgements

1. Introduction

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

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 - Sets 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 - Sets 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 - Sets 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/sets
      

Throughout this document, the prefix set 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. Set Functions

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

Issue: EXSLT 1.0 - Sets 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: node-set set:difference(node-set, node-set)

The set:difference function returns the difference between two node sets - those nodes that are in the node set passed as the first argument that are not in the node set passed as the second argument.

Function: boolean set:has-same-node(node-set, node-set)

The set:has-same-node function returns true if the node set passed as the first argument shares any nodes with the node set passed as the second argument. If there are no nodes that are in both node sets, then it returns false.

Function: node-set set:intersection(node-set, node-set)

The set:intersection function returns a node set comprising the nodes that are within both the node sets passed as arguments to it.

Function: node-set set:distinct(node-set, string?)

The set:distinct function returns the nodes within the node set passed as the first argument that have different values. 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 set:leading(node-set, string)

The set:leading function returns the nodes in the node set passed as the first argument that precede, in document order, the first node in the node set whose value evaluates to true. 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 set:following(node-set, string)

The set:following function returns the nodes in the node set passed as the first argument that is itself or that follow, in document order, the first node in the node set whose value is true. 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: boolean set:exists(node-set, string?)

The set:exists function returns true if the value of any of the nodes in the node set passed as the first argument is true. 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: boolean set:for-all(node-set, string?)

The set:for-all function returns true if the value of all the nodes in the node set passed as the first argument is true. 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: node-set set:difference(node-set, node-set)

<exsl:function name="set:difference">
   <xsl:param name="node-set1" select="/.." />
   <xsl:param name="node-set2" select="/.." />
   <exsl:result select="$node-set1[count(.|$node-set2) != count($node-set2)]" />
</exsl:function>
      

Function: boolean set:has-same-node(node-set, node-set)

<exsl:function name="set:has-same-node">
   <xsl:param name="node-set1" select="/.." />
   <xsl:param name="node-set2" select="/.." />
   <exsl:result
      select="boolean($node-set1[count(.|$node-set2) = count($node-set2)])" />
</exsl:function>
      

Function: node-set set:intersection(node-set, node-set)

<exsl:function name="set:intersection">
   <xsl:param name="node-set1" select="/.." />
   <xsl:param name="node-set2" select="/.." />
   <exsl:result select="$node-set1[count(.|$node-set2) = count($node-set2)]" />
</exsl:function>
      

Function: node-set set:distinct(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 set:distinct.

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

Function: node-set set:leading(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 set:leading.

<exsl:function name="set:leading">
   <xsl:param name="node-set" select="/.." />
   <xsl:param name="expr" select="'.'" />
   <xsl:choose>
      <xsl:when test="not($node-set)">
         <exsl:result select="/.." />
      <xsl:when>
      <xsl:otherwise>
         <xsl:variable name="value"
                          select="string(com:eval($node-set[1], $expr))" />
         <xsl:choose>
            <xsl:when test="$value = 'false' or not($value)">
               
               <exsl:result select="$node-set[1] | 
                                       set:leading($node-set[position() != 1], $expr)" />
            </xsl:when>
            <xsl:otherwise>
               <exsl:result select="/.." />
            </xsl:otherwise>
         </xsl:choose>
      </xsl:otherwise>
   </xsl:choose>
</exsl:function>
      

Function: node-set set:following(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 set:following.

<exsl:function name="set:following">
   <xsl:param name="node-set" select="/.." />
   <xsl:param name="expr" select="'.'" />
   <xsl:choose>
      <xsl:when test="not($node-set)">
         <exsl:result select="/.." />
      </xsl:when>
      <xsl:otherwise>
         <xsl:variable name="value" 
                          select="string(com:eval($node-set[1], $expr))" />
         <xsl:choose>
            <xsl:when test="$value = 'false' or not($value)">
               <exsl:result select="set:following($node-set[position() != 1],
                                                     $expr)" />
            </xsl:when>
            <xsl:otherwise>
               <exsl:result select="$node-set" />
            </xsl:otherwise>
         </xsl:choose>
      </xsl:otherwise>
   </xsl:choose>
</exsl:function>
      

Function: boolean set:exists(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 set:exists.

<exsl:function name="set:exists">
   <xsl:param name="node-set" select="/.." />
   <xsl:param name="expr" select="'.'" />
   <xsl:choose>
      <xsl:when test="not($node-set)">
         <exsl:result select="false()" />
      </xsl:when>
      <xsl:otherwise>
         <xsl:variable name="value" 
                          select="string(com:eval($node-set[1], $expr))" />
         <xsl:choose>
            <xsl:when test="$value = 'false' or not($value)">
               <exsl:result select="set:exists($node-set[position() != 1],
                                                  $value, $expr)" />
            </xsl:when>
            <xsl:otherwise>
               <exsl:result select="true()" />
            </xsl:otherwise>
         </xsl:choose>
      </xsl:otherwise>
   </xsl:choose>
</exsl:function>
      

Function: boolean set:for-all(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 set:for-all.

<exsl:function name="set:for-all">
   <xsl:param name="node-set" select="/.." />
   <xsl:param name="expr" select="'.'" />
   <xsl:choose>
      <xsl:when test="not($node-set)">
         <exsl:result select="true()" />
      </xsl:when>
      <xsl:otherwise>
         <xsl:variable name="value"
                          select="string(com:eval($node-set[1], $expr))" />
         <xsl:choose>
            <xsl:when test="$value = 'false' or not($value)">
               <exsl:result select="false()" />
            </xsl:when>
            <xsl:otherwise>
               <exsl:result select="set:for-all($node-set[position() != 1],
                                                   $value, $expr)" />
            </xsl:otherwise>
         </xsl:choose>
      </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.