• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?xml version="1.0" encoding="iso-8859-1" ?>
2<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
3<xsl:output method="xml"/>
4
5  <!-- FileName: sort36 -->
6  <!-- Document: http://www.w3.org/TR/xslt -->
7  <!-- DocVersion: 19991116 -->
8  <!-- Section: 10 -->
9  <!-- Creator: David Marston -->
10  <!-- Purpose: Test the famous technique for sorting with conditionals.
11     In this case, we want to sort strings with and without "Re: " prefix by
12     the rest of the string. Technique from Oliver Becker (obecker@informatik.hu-berlin.de). -->
13
14<xsl:template match="/">
15  <out><xsl:text>Memos by thread:
16</xsl:text>
17    <xsl:for-each select="doc/memo">
18      <xsl:sort select="concat(
19        substring(subj,1,number(not(starts-with(.,'Re: ')))*string-length(subj)),
20        substring(substring-after(subj,'Re: '),1,
21          number(starts-with(.,'Re: '))*string-length(substring-after(subj,'Re: '))))"
22        data-type="text" />
23        <!-- First substring in concat is null string when 'Re: ' is at start, all of subj otherwise.
24          Second substring in concat is null at opposite times from first, when non-null it's all of subj beyond 'Re: '.
25          Thus, concat "chooses" which string (all of subj or trimmed subj) based on the boolean starts-with(.,'Re: '),
26          because one of the two substring functions returns a null string while other doesn't.
27          The substring(x,1,0) returns null and substring(x,1,string-length(x)) returns the whole x.
28          The zero comes from converting the boolean false to a number, where multiplying by string-length stays zero.
29          The other branch gets boolean true converted to 1, then multiplied by string-length. -->
30      <xsl:sort select="time" data-type="text" /><!-- Tie breaker, for demonstration only. -->
31      <xsl:value-of select="body"/><xsl:text>: </xsl:text><xsl:value-of select="subj"/><xsl:text>;
32</xsl:text>
33    </xsl:for-each>
34  </out>
35</xsl:template>
36
37
38  <!--
39   * Licensed to the Apache Software Foundation (ASF) under one
40   * or more contributor license agreements. See the NOTICE file
41   * distributed with this work for additional information
42   * regarding copyright ownership. The ASF licenses this file
43   * to you under the Apache License, Version 2.0 (the  "License");
44   * you may not use this file except in compliance with the License.
45   * You may obtain a copy of the License at
46   *
47   *     http://www.apache.org/licenses/LICENSE-2.0
48   *
49   * Unless required by applicable law or agreed to in writing, software
50   * distributed under the License is distributed on an "AS IS" BASIS,
51   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
52   * See the License for the specific language governing permissions and
53   * limitations under the License.
54  -->
55
56</xsl:stylesheet>
57