eXforms - extending XForms 1.0

14 December 2006

This version:
http://exforms.org/sorting.html
Editor:
David Landwehr, exforms.org

Status of this Document

This module provides an element and XPath functions for sorting XPath node-sets.

Table of Contents

1 Sorting
    1.1 The Module
    1.2 The exf:sort Element
    1.3 The exf:sort Function
2 References
    2.1 Normative References

Appendix

A Acknowledgments (Non-Normative)


1 Sorting

1.1 The Module

This module provides an element and XPath functions for sorting XPath node-sets.

1.2 The exf:sort Element

The sort elements are placed as the first children under a XForms element which contains a node-set binding. The first sort element specifies the primary sorting and only if two nodes are equal the secondary sort element will be used and so on. If two nodes are said to be equal and there are no more sort elements to specify their internal order then the order of the two nodes are the document node order.

Attributes:

select

Required [XPath 1.0] expression evaluation to the value which is used for sorting. The evaluation scope for this attribute is the one set by the nodeset attribute.

lang

Optional specifies the locale of the sorting keys. If the attribute is absent the a system default is used.

data-type

Optional one of text, number or a qname-but-not-ncname. The default is text.

order

Optional one of ascending or descending. Specifies the sorting order where ascending specifies ascending order and descending specifies descending order. If absent then ascending is the default value.

case-order

Optional one of lower-first or upper-first. If lower-first then lower case characters are before upper-case characters. This attribute applies only when data-type="text". If absent upper-first is the default value.

Example: how to use exf:sort
<xforms:model>
  <xforms:instance>
    <data xmlns="">
      <node value="2">Node 1</node>
      <node value="1">Node 2</node>
    </data>
  </xforms:instance>
</xforms:model>

<xforms:repeat nodeset="node">
  <exf:sort select="@value" data-type="number"/>
  <xforms:output ref="."/>
</xforms:repeat>

Will output Node 2 then Node 1

Example: how to use exf:sort to reverse document order
<xforms:model>
  <xforms:instance>
    <data xmlns="">
      <node value="2">Node 1</node>
      <node value="1">Node 2</node>
    </data>
  </xforms:instance>
</xforms:model>

<xforms:repeat nodeset="node">
  <exf:sort select="position()" data-type="number" order="descending"/>
  <xforms:output ref="."/>
</xforms:repeat>

Will reverse the document order

Example: how to use exf:sort in select1
<xforms:model>
  <xforms:instance>
    <addressbook xmlns="">
      <person>
        <name>Smidth, John</name>
        <street>204 Street</street>
        <age>21</age>
      </person>
      <person>
        <name>Smidth, Alice</name>
        <street>204 Street</street>
        <age>23</age>
      </person>
      <person>
        <name>Seins, Jon</name>
        <street>202 Street</street>
        <age>18</age>
      </person>
      <selected/>
    </addressbook>
  </xforms:instance>
</xforms:model>

<xforms:select1 ref="selected">
  <xforms:label>Selected person:</xforms:label>
  <xforms:itemset nodeset="/addressbook/person">
    <exf:sort select="street"/>
    <exf:sort select="age"/>
    <exf:sort select="name"/>
    
    <xforms:label ref="name"/>
    <xforms:copy ref="."/>
  </xforms:itemset>
</xforms:select1>

Sorts the itemset in the select1 in the order street, age, name

1.3 The exf:sort Function

node-set exf:sort(node-set, string, string?, string?, string?)

The exf:sort function sorts a node-set provided as the first argument using an XPath expression given as the second argument. The sorting expression evaluates to a string which is used as a XPath expression created with the same context as created the expression containing the exf:sort function. The third argument specifies the data-type of the sorting expression (one of number or text anything but one of these two values will default the value to text), e.g. if it should evaluate to a number or string. The fourth argument specifies the ordering of the sorting (one of ascending of descending anything but one of these two values will default the value to ascending. The fifth element is used when the data-type was text to specify the case order. The argument should be one of lower-first or upper-first anything but one of these two values will default the argument to lower-first. If two nodes does not have an internal sorting they must maintain the order they had from the incoming node-set (the first argument).

Notice that the use of this function makes it possible to change the way the sorting is done in runtime. This isn't possible with the sorting element. E.g. by storing different expressions in an instance it is possible to select a different expression for the sorting.

Example: how to use exf:sort
<xforms:model>
  <xforms:instance>
    <data xmlns="">
      <node value="2">Node 1</node>
      <node value="1">Node 2</node>
    </data>
  </xforms:instance>
</xforms:model>

<xforms:repeat nodeset="exf:sort(node, '@value', 'number')">
  <xforms:output ref="."/>
</xforms:repeat>

Will output Node 2 then Node 1

Example: how to use exf:sort to reverse document order
<xforms:model>
  <xforms:instance>
    <data xmlns="">
      <node value="2">Node 1</node>
      <node value="1">Node 2</node>
    </data>
  </xforms:instance>
</xforms:model>

<xforms:repeat nodeset="exf:sort(node, 'position()', 'number', 'descending')">
  <xforms:output ref="."/>
</xforms:repeat>
Example: how to use exf:sort to change the sorting
<xforms:model>
  <xforms:instance>
    <data xmlns="" use-sort="1">
      <node value="2">Node 1</node>
      <node value="1">Node 2</node>
      <sort id="1">position()</sort>
      <sort id="2">@value</sort>
    </data>
  </xforms:instance>
</xforms:model>

<xforms:repeat nodeset="exf:sort(node, sort[/data/@use-sort=@id], 'number')">
  <xforms:output ref="."/>
</xforms:repeat>

<xforms:select1 ref="@use-sort">
  <xforms:label>Sort method:</xforms:label>
  <xforms:itemset nodeset="sort">
    <xforms:label ref="."/>
    <xforms:value ref="@id">
  </xforms:itemset>
</xforms:select1>

2 References

2.1 Normative References

XForms 1.0
XForms - Version 1.0, John M. Boyer, David Landwehr, Roland Merrick, T. V. Raman, Micah Dubinko, Leigh L. Klotz, Jr., 2006. W3C Recommendation available at: http://www.w3.org/TR/xforms/.
XML Events
XML Events - An events syntax for XML, Steven Pemberton, T. V. Raman, Shane P. McCarron, 2002. W3C Recommendation available at: http://www.w3.org/TR/xml-events/.
XPath 1.0
XML Path Language (XPath) Version 1.0, James Clark, Steve DeRose, 1999. W3C Recommendation available at: http://www.w3.org/TR/xpath.
XML Schema part 1
XML Schema Part 1: Structures, Henry S. Thompson, David Beech, Murray Maloney, Noah Mendelsohn, 2001. W3C Recommendation available at: http://www.w3.org/TR/xmlschema-1/.
XML Schema part 2
XML Schema Part 2: Datatypes, Paul V. Biron, Ashok Malhotra, 2001. W3C Recommendation available at: http://www.w3.org/TR/xmlschema-2/.
RFC 2119
RFC 2119: Key words for use in RFCs to Indicate Requirement Levels, S. Bradner, 1997. Available at http://www.ietf.org/rfc/rfc2119.txt.

A Acknowledgments (Non-Normative)

eXforms.org would like to thank the following for their contributions in creating the extensions:

  • David Landwehr, exforms.org / SolidApp
  • Kenneth Sklander, picoforms.com
  • Allan Beaufour, beaufour.dk