Statistics
| Revision:

root / tmp / org.txm.core / res / org / txm / xml / xsl / tei / odt / grouping.xsl @ 187

History | View | Annotate | Download (4.3 kB)

1
<xsl:stylesheet
2
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
4
 xmlns:mf="http://example.com/mf"
5
 exclude-result-prefixes="xs mf"
6
 version="2.0">
7
  <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet" type="stylesheet">
8
      <desc>
9
         <p> TEI stylesheet dealing with elements from the core module. </p>
10
         <p>This software is dual-licensed:
11

    
12
1. Distributed under a Creative Commons Attribution-ShareAlike 3.0
13
Unported License http://creativecommons.org/licenses/by-sa/3.0/ 
14

    
15
2. http://www.opensource.org/licenses/BSD-2-Clause
16
		
17
All rights reserved.
18

    
19
Redistribution and use in source and binary forms, with or without
20
modification, are permitted provided that the following conditions are
21
met:
22

    
23
* Redistributions of source code must retain the above copyright
24
notice, this list of conditions and the following disclaimer.
25

    
26
* Redistributions in binary form must reproduce the above copyright
27
notice, this list of conditions and the following disclaimer in the
28
documentation and/or other materials provided with the distribution.
29

    
30
This software is provided by the copyright holders and contributors
31
"as is" and any express or implied warranties, including, but not
32
limited to, the implied warranties of merchantability and fitness for
33
a particular purpose are disclaimed. In no event shall the copyright
34
holder or contributors be liable for any direct, indirect, incidental,
35
special, exemplary, or consequential damages (including, but not
36
limited to, procurement of substitute goods or services; loss of use,
37
data, or profits; or business interruption) however caused and on any
38
theory of liability, whether in contract, strict liability, or tort
39
(including negligence or otherwise) arising in any way out of the use
40
of this software, even if advised of the possibility of such damage.
41
</p>
42
         <p>Author: See AUTHORS</p>
43
         <p>Id: $Id: grouping.xsl 9646 2011-11-05 23:39:08Z rahtz $</p>
44
         <p>Copyright: 2008, TEI Consortium</p>
45
      </desc>
46
   </doc>
47

    
48

    
49
 <xsl:output indent="yes"/>
50

    
51
 <xsl:template match="body">
52
   <xsl:copy>
53
     <xsl:sequence select="mf:group(*, 1, 0)"/>
54
   </xsl:copy>
55
 </xsl:template>
56

    
57
 <xsl:function name="mf:group" as="node()*">
58
   <xsl:param name="elements" as="element()*"/>
59
   <xsl:param name="level" as="xs:integer"/>
60
   <xsl:param name="last-level" as="xs:integer"/>
61
   <xsl:for-each-group select="$elements"
62
group-starting-with="head[@level = $level]">
63
     <xsl:variable name="head" as="element()?"
64
select=".[self::head[@level = $level]]"/>
65
     <xsl:variable name="tail" as="element()*" select="current-group()
66
except $head"/>
67
     <xsl:choose>
68
       <xsl:when test="$head">
69
         <xsl:sequence select="mf:nested-divs($head, $tail, $level,
70
$level - $last-level)"/>
71
       </xsl:when>
72
       <xsl:otherwise>
73
           <xsl:choose>
74
             <xsl:when test="$tail[self::head[@level]]">
75
               <xsl:sequence select="mf:group($tail, $level + 1, $last-level)"/>
76
             </xsl:when>
77
             <xsl:otherwise>
78
               <xsl:copy-of select="$tail"/>
79
             </xsl:otherwise>
80
           </xsl:choose>
81
       </xsl:otherwise>
82
     </xsl:choose>
83
   </xsl:for-each-group>
84
 </xsl:function>
85

    
86
 <xsl:function name="mf:nested-divs" as="element()*">
87
   <xsl:param name="head" as="element()?"/>
88
   <xsl:param name="tail" as="element()*"/>
89
   <xsl:param name="level" as="xs:integer"/>
90
   <xsl:param name="i" as="xs:integer"/>
91
   <xsl:choose>
92
     <xsl:when test="$i gt 0">
93
       <div>
94
         <xsl:sequence select="mf:nested-divs($head, $tail, $level, $i - 1)"/>
95
       </div>
96
     </xsl:when>
97
     <xsl:otherwise>
98
       <xsl:copy-of select="$head"/>
99
       <xsl:choose>
100
         <xsl:when test="$tail[self::head[@level]]">
101
           <xsl:sequence select="mf:group($tail, $level + 1, $level)"/>
102
         </xsl:when>
103
         <xsl:otherwise>
104
           <xsl:copy-of select="$tail"/>
105
         </xsl:otherwise>
106
       </xsl:choose>
107
     </xsl:otherwise>
108
   </xsl:choose>
109
 </xsl:function>
110

    
111
</xsl:stylesheet>
112

    
113
<!--
114
It has become rather complicated with two functions due to the
115
requirement to add missing levels.
116

    
117
Your latest suggestion to first normalize the input by adding missing
118
levels and then to group in a second pass might indeed be an approach
119
leading to shorter and less complicated code.
120

    
121

    
122

    
123
       Martin Honnen
124
       http://msmvps.com/blogs/martin_honnen/
125
-->
126