Extensions to XSLT 1.0 (EXSLT 1.0) - Sets

This version: exslt-sets-010310.html
Most Recent version: http://www.jenitennison.com/xslt/exslt/sets/
Previous version: exslt-sets-010302.html
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
D. Changes from Previous Version

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].

XSLT processors are free to support any number of the extension elements and functions described in this document. However, an XSLT processor must not claim to support EXSLT 1.0 - Sets unless all the extensions described within this document are implemented by the processor. An implementation of an extension element or function in an EXSLT namespace must conform to the behaviour described in this document.

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.

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)

The set:distinct function returns the nodes within the node set passed as the first argument that are the first nodes in document order whose string value is their string value.

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

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 passed as the second argument. If the first node in the second node set is not contained in the first node set, then an empty node set is returned. If the second node set is empty, then the first node set is returned.

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

The set:trailing function returns the nodes in the node set passed as the first argument that follow, in document order, the first node in the node set passed as the second argument. If the first node in the second node set is not contained in the first node set, then an empty node set is returned. If the second node set is empty, then the first node set is returned.

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)

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

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

<exsl:function name="set:leading">
   <xsl:param name="node-set" select="/.." />
   <xsl:param name="end-node-set" select="/.." />
   <xsl:variable name="end-node" select="$end-node-set[1]" />
   <xsl:choose>
      <xsl:when test="not($end-node) or not($node-set)">
         <exsl:result select="$node-set" />
      </xsl:when>
      <xsl:when test="not(set:has-same-node($node-set, $end-node)) or 
                      count($node-set[1] | $end-node) = 1">
         <exsl:result select="/.." />
      </xsl:when>
      <xsl:otherwise>
         <exsl:result select="$node-set[1] | 
                              set:leading($node-set[position() > 1], $end-node)" />
      </xsl:otherwise>
   </xsl:choose>
</exsl:function>
      

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

<exsl:function name="set:trailing">
   <xsl:param name="node-set" select="/.." />
   <xsl:param name="end-node-set" select="/.." />
   <xsl:variable name="end-node" select="$end-node-set[1]" />
   <xsl:choose>
      <xsl:when test="not($end-node) or not($node-set)">
         <exsl:result select="$node-set" />
      </xsl:when>
      <xsl:when test="not(set:has-same-node($node-set, $end-node))">
         <exsl:result select="/.." />
      </xsl:when>
      <xsl:when test="count($node-set[1] | $end-node) = 1">
         <exsl:result select="$node-set[position() > 1]" />
      </xsl:when>
      <xsl:otherwise>
         <exsl:result select="set:trailing($node-set[position() > 1], $end-node)" />
      </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.

Also many thanks to Uche Ogbuji for his comments following implementation of these functions in 4XSLT.

D. Changes from Previous Version