Altova StyleVision 2024 Professional Edition

The Sorting Mechanism

Home Prev Top Next

Setting up a schema element node for sorting consists of two steps:


1.In Design View, select the schema element node that is to be sorted. Note that it is the instances of this element in the XML document that will be sorted. Often it might not immediately be apparent which element is to be sorted. For example, consider the structure shown in the screenshot below.




Each newsitem has a dateline containing a place element with a city attribute. The @city nodes of all newsitem elements are to be output in alphabetical order. In the design, should the @city node be selected for sorting, or the place, dateline, or newsitem elements? With @city selected, there will be only the one city node that will be sorted. With place or dateline selected, again there will be just the one respective element to sort, since within their parents they occur singly. With newsitem selected, however, there will be multiple newsitem elements within the parent newsitems element. In this case, it is the newsitem element that should be sorted, using a sort-key of dateline/place/@city.

2.After selecting the element to sort, in the context menu (obtained by right-clicking the element selection), click the Sort Output command. This pops up the Define Output Sort Order dialog (screenshot below), in which you insert or append one or more sort instructions.




Each sort instruction contains: (i) a sort-key (entered in the Match column); (ii) the datatype that the sort-key node should be considered to be (text or number); (iii) and the order of the sorting (ascending or descending). The order in which the sort instructions are listed is significant. Sorting is carried out using each sort instruction in turn, starting with the first, and working down the list when multiple items have the same value. Any number of sort instructions are allowed.


For an example of how sorting is used, see Example: Sorting on Multiple Sort-Keys.


User-defined templates

User-defined templates are templates that are applied to items selected by an XPath expression you specify. The nodes selected by the XPath expression of a user-defined template can also be sorted. In this case, the sorting is applied on the user-defined template.


A note about sort-keys

The XPath expression you enter for the sort-key must select a single node for each element instance—not a nodeset (XPath 1.0) or a sequence of items (XPath 2.0 and XPath 3.0); the key for each element should be resolvable to a string or number value.


In an XSLT 2.0 or 3.0 SPS, if the sort-key returns a sequence of nodes, an XSLT processing error will be returned. So, in the Person example cited above, with a context node of Person, an XPath expression such as: ../Person/LastName would return an error because this expression returns all the LastName elements contained in the parent of Person (assuming there is more than one Person element). The correct XPath expression, with Person as the context node, would be: LastName (since there is only one LastName node for each Person element).


In XSLT 1.0, the specification requires that when a nodeset is returned by the sort-key selector, the text value of the first node is used. StyleVision therefore returns no error if the XPath expression selects multiple nodes for the sort-key; the text of the first node is used and the other nodes are ignored. However, the first node selected might not be the desired sort-key. For example, the XPath expression ../Person/LastName of the example described above would not return an error. But neither would it sort, because it is the same value for each element in the entire sort loop (the text value of the first LastName node). An expression of the kind: location/@*, however, would sort, using the first attribute of the location child element as the sort-key. This kind of expression, however, is to be avoided, and a more precise selection of the sort-key (selecting a single node) is advised.


© 2018-2024 Altova GmbH