Revision 2398 tmp/org.txm.oriflamms.rcp/res/1-oriflamms-split-surfaces.xsl

1-oriflamms-split-surfaces.xsl (revision 2398)
1 1
<?xml version="1.0" encoding="utf-8"?>
2 2
<xsl:stylesheet
3
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4
		xmlns:xd="http://www.pnp-software.com/XSLTdoc"
5
		xmlns:tei="http://www.tei-c.org/ns/1.0"
6
		version="2.0" xpath-default-namespace="http://www.tei-c.org/ns/1.0">
3
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4
	xmlns:xd="http://www.pnp-software.com/XSLTdoc"
5
	xmlns:tei="http://www.tei-c.org/ns/1.0" version="2.0"
6
	xpath-default-namespace="http://www.tei-c.org/ns/1.0">
7 7

  
8
  <xd:doc type="stylesheet">
9
    <xd:short>
10
      Cette feuille permet de découper un fichier XML TEI Oriflamms en pages
11
      avant de procéder à la tokénisation au niveau de caractère.
12
    </xd:short>
13
    <xd:detail>
14
      This software is dual-licensed:
15
      
16
      1. Distributed under a Creative Commons Attribution-ShareAlike 3.0
17
      Unported License http://creativecommons.org/licenses/by-sa/3.0/ 
18
      
19
      2. http://www.opensource.org/licenses/BSD-2-Clause
20
      
21
      All rights reserved.
22
      
23
      Redistribution and use in source and binary forms, with or without
24
      modification, are permitted provided that the following conditions are
25
      met:
26
      
27
      * Redistributions of source code must retain the above copyright
28
      notice, this list of conditions and the following disclaimer.
29
      
30
      * Redistributions in binary form must reproduce the above copyright
31
      notice, this list of conditions and the following disclaimer in the
32
      documentation and/or other materials provided with the distribution.
33
      
34
      This software is provided by the copyright holders and contributors
35
      "as is" and any express or implied warranties, including, but not
36
      limited to, the implied warranties of merchantability and fitness for
37
      a particular purpose are disclaimed. In no event shall the copyright
38
      holder or contributors be liable for any direct, indirect, incidental,
39
      special, exemplary, or consequential damages (including, but not
40
      limited to, procurement of substitute goods or services; loss of use,
41
      data, or profits; or business interruption) however caused and on any
42
      theory of liability, whether in contract, strict liability, or tort
43
      (including negligence or otherwise) arising in any way out of the use
44
      of this software, even if advised of the possibility of such damage.
45
      
46
      $Id$
47
      
48
      This stylesheet is based on TEI processpb.xsl by Sebastian Rahtz 
49
      available at 
50
      https://github.com/TEIC/Stylesheets/blob/master/tools/processpb.xsl 
51
      and is adapted by Alexei Lavrentiev to split an XML TEI Oriflamms file
52
      into pages for further tokenizatrion at character level.
53
      
54
    </xd:detail>
55
    <xd:author>Alexei Lavrentiev alexei.lavrentev@ens-lyon.fr</xd:author>
56
    <xd:copyright>2015, CNRS / ICAR (Équipe CACTUS)</xd:copyright>
57
  </xd:doc>
58
  
59
  
60
  
61
  <xsl:output indent="no" method="xml"/>
8
	<xd:doc type="stylesheet">
9
		<xd:short>
10
			Cette feuille permet de découper un fichier XML TEI Oriflamms en pages
11
			avant de procéder à la tokénisation au niveau de caractère.
12
		</xd:short>
13
		<xd:detail>
14
			This software is dual-licensed:
62 15

  
16
			1. Distributed under a Creative Commons Attribution-ShareAlike 3.0
17
			Unported License http://creativecommons.org/licenses/by-sa/3.0/
63 18

  
64
  <xsl:variable name="filename">
65
    <xsl:analyze-string select="document-uri(.)" regex="^(.*)/([^/]+)\.[^/]+$">
66
      <xsl:matching-substring>
67
        <xsl:value-of select="regex-group(2)"/>
68
      </xsl:matching-substring>
69
    </xsl:analyze-string>
70
  </xsl:variable>
71
  
72
  <xsl:variable name="filedir">
73
    <xsl:analyze-string select="document-uri(.)" regex="^(.*)/([^/]+)\.[^/]+$">
74
      <xsl:matching-substring>
75
        <xsl:value-of select="regex-group(1)"/>
76
      </xsl:matching-substring>
77
    </xsl:analyze-string>
78
  </xsl:variable>
79
  
80
  <!--<xsl:variable name="filenameresult">
81
    <xsl:value-of select="replace($filename,'(-ori)?-w$','')"/>
82
  </xsl:variable>-->
19
			2. http://www.opensource.org/licenses/BSD-2-Clause
83 20

  
84
  <xsl:param name="output-directory"><xsl:value-of select="concat($filedir,'/out')"/></xsl:param>
21
			All rights reserved.
85 22

  
86
 
87
  <xsl:template match="teiHeader">
88
    <!--<xsl:copy-of select="."/>-->
89
  </xsl:template>
90
  
91
  <xsl:template match="TEI|teiCorpus|group|text">
92
    <xsl:copy>
93
      <xsl:apply-templates select="@*"/>
94
      <xsl:apply-templates select="*|processing-instruction()|comment()|text()"/>
95
    </xsl:copy>
96
  </xsl:template>
23
			Redistribution and use in source and binary forms, with or without
24
			modification, are permitted provided that the following conditions are
25
			met:
97 26

  
27
			* Redistributions of source code must retain the above copyright
28
			notice, this list of conditions and the following disclaimer.
98 29

  
99
  <xsl:template match="text/body|text/back|text/front">
100
      <xsl:variable name="pages">
101
	<xsl:copy>
102
	  <xsl:apply-templates select="@*"/>
103
	  <xsl:apply-templates
104
	      select="*|processing-instruction()|comment()|text()"/>
105
	</xsl:copy>
106
      </xsl:variable>
107
      <xsl:for-each select="$pages">
108
	<xsl:apply-templates  mode="pass2"/>
109
      </xsl:for-each>
110
  </xsl:template>
30
			* Redistributions in binary form must reproduce the above copyright
31
			notice, this list of conditions and the following disclaimer in the
32
			documentation and/or other materials provided with the distribution.
111 33

  
34
			This software is provided by the copyright holders and contributors
35
			"as is" and any express or implied warranties, including, but not
36
			limited to, the implied warranties of merchantability and fitness for
37
			a particular purpose are disclaimed. In no event shall the copyright
38
			holder or contributors be liable for any direct, indirect,
39
			incidental,
40
			special, exemplary, or consequential damages (including, but not
41
			limited to, procurement of substitute goods or services; loss of use,
42
			data, or profits; or business interruption) however caused and on any
43
			theory of liability, whether in contract, strict liability, or tort
44
			(including negligence or otherwise) arising in any way out of the use
45
			of this software, even if advised of the possibility of such damage.
112 46

  
113
 <!-- first (recursive) pass. look for <pb> elements and group on them -->
114
  <xsl:template match="comment()|@*|processing-instruction()|text()">
115
    <xsl:copy-of select="."/>
116
  </xsl:template>
47
			$Id$
117 48

  
118
  <xsl:template match="*">
119
    <xsl:call-template name="checkpb">
120
      <xsl:with-param name="eName" select="local-name()"/>
121
    </xsl:call-template>
122
  </xsl:template>
49
			This stylesheet is based on TEI processpb.xsl by Sebastian Rahtz
50
			available at
51
			https://github.com/TEIC/Stylesheets/blob/master/tools/processpb.xsl
52
			and is adapted by Alexei Lavrentiev to split an XML TEI Oriflamms
53
			file
54
			into pages for further tokenizatrion at character level.
123 55

  
124
  <xsl:template match="milestone[@unit='surface']">
125
    <xsl:copy-of select="."/>
126
  </xsl:template>
56
		</xd:detail>
57
		<xd:author>Alexei Lavrentiev alexei.lavrentev@ens-lyon.fr</xd:author>
58
		<xd:copyright>2015, CNRS / ICAR (Équipe CACTUS)</xd:copyright>
59
	</xd:doc>
127 60

  
128
  <xsl:template name="checkpb">
129
    <xsl:param name="eName"/>
130
    <xsl:choose>
131
      <xsl:when test="not(.//milestone[@unit='surface'])">
132
        <xsl:copy-of select="."/>
133
      </xsl:when>
134
      <xsl:otherwise>
135
        <xsl:variable name="pass">
136
	  <xsl:call-template name="groupbypb">
137
	    <xsl:with-param name="Name" select="$eName"/>
138
	  </xsl:call-template>
139
        </xsl:variable>
140
	<xsl:for-each select="$pass">
141
	  <xsl:apply-templates/>
142
	</xsl:for-each>
143
      </xsl:otherwise>
144
    </xsl:choose>
145
  </xsl:template>
146 61

  
147
  <xsl:template name="groupbypb">
148
    <xsl:param name="Name"/>
149
    <xsl:for-each-group select="node()" group-starting-with="milestone[@unit='surface']">
150
      <xsl:choose>
151
        <xsl:when test="self::milestone[@unit='surface']">
152
          <xsl:copy-of select="."/>
153
          <xsl:element name="{$Name}" namespace="http://www.tei-c.org/ns/1.0">
154
	    <xsl:attribute name="rend">CONTINUED</xsl:attribute>
155
            <xsl:apply-templates select="current-group() except ."/>
156
          </xsl:element>
157
        </xsl:when>
158
        <xsl:otherwise>
159
          <xsl:element name="{$Name}" namespace="http://www.tei-c.org/ns/1.0">
160
            <xsl:for-each select="..">
161
              <xsl:copy-of select="@*"/>
162
              <xsl:apply-templates select="current-group()"/>
163
            </xsl:for-each>
164
          </xsl:element>
165
        </xsl:otherwise>
166
      </xsl:choose>
167
    </xsl:for-each-group>
168
  </xsl:template>
169 62

  
170
  <!-- second pass. group by <pb> (now all at top level) and wrap groups
171
       in <page> -->
172
  <xsl:template match="*" mode="pass2">
173
    <xsl:copy>
174
      <xsl:apply-templates select="@*|*|processing-instruction()|comment()|text()" mode="pass2"/>
175
    </xsl:copy>
176
  </xsl:template>
63
	<xsl:output indent="no" method="xml" />
177 64

  
178
  <xsl:template match="comment()|@*|processing-instruction()|text()" mode="pass2">
179
    <xsl:copy-of select="."/>
180
  </xsl:template>
181 65

  
66
	<xsl:variable name="filename">
67
		<xsl:analyze-string select="document-uri(.)"
68
			regex="^(.*)/([^/]+)\.[^/]+$">
69
			<xsl:matching-substring>
70
				<xsl:value-of select="regex-group(2)" />
71
			</xsl:matching-substring>
72
		</xsl:analyze-string>
73
	</xsl:variable>
182 74

  
183
  <xsl:template match="*[milestone[@unit='surface']]" mode="pass2" >
184
    <xsl:copy>
185
      <xsl:apply-templates select="@*"/>
186
      <xsl:for-each-group select="*" group-starting-with="milestone[@unit='surface']">
187
        <xsl:choose>
188
          <xsl:when test="self::milestone[@unit='surface']">
189
          	<xsl:comment> Page <xsl:value-of select="@xml:id"/> enregistrée dans <xsl:value-of select="concat($output-directory,'/',$filename,'_',@xml:id,'.xml')"/></xsl:comment>
190
            <xsl:text>&#xA;</xsl:text>
191
          	<xsl:result-document href="{$output-directory}/{$filename}_{@xml:id}.xml" indent="yes" exclude-result-prefixes="#all">
192
          	<TEI xmlns="http://www.tei-c.org/ns/1.0">
193
          	  <teiHeader>
194
          	    <fileDesc>
195
          	      <titleStmt>
196
          	        <title>Page <xsl:value-of select="@xml:id"/> of <xsl:value-of select="$filename"/> tokenized at character level.</title>
197
          	      </titleStmt>
198
          	      <publicationStmt>
199
          	        <p>Oriflamms project (http://oriflamms.hypotheses.org)</p>
200
          	      </publicationStmt>
201
          	      <sourceDesc>
202
          	        <p>Converted from <xsl:value-of select="$filename"/>.</p>
203
          	      </sourceDesc>
204
          	    </fileDesc>
205
          	    <revisionDesc>
206
          	      <change when="{current-dateTime()}">File created.</change>
207
          	    </revisionDesc>
208
          	  </teiHeader>
209
          	  <text>
210
          	    <body>
211
          	      <xsl:copy-of select="."/>
212
        	        <xsl:copy-of select="current-group() except ."/>
213
          	    </body>
214
          	  </text>
215
          	</TEI>
216
          	</xsl:result-document>
217
          </xsl:when>
218
          <xsl:otherwise>
219
            <xsl:copy-of select="current-group()"/>
220
          </xsl:otherwise>
221
        </xsl:choose>
222
      </xsl:for-each-group>
223
    </xsl:copy>
224
  </xsl:template>
75
	<xsl:variable name="filedir">
76
		<xsl:analyze-string select="document-uri(.)"
77
			regex="^(.*)/([^/]+)\.[^/]+$">
78
			<xsl:matching-substring>
79
				<xsl:value-of select="regex-group(1)" />
80
			</xsl:matching-substring>
81
		</xsl:analyze-string>
82
	</xsl:variable>
225 83

  
84
	<!--<xsl:variable name="filenameresult"> <xsl:value-of select="replace($filename,'(-ori)?-w$','')"/> 
85
		</xsl:variable> -->
86

  
87
	<xsl:param name="output-directory">
88
		<xsl:value-of select="concat($filedir,'/out')" />
89
	</xsl:param>
90

  
91

  
92
	<xsl:template match="teiHeader">
93
		<!--<xsl:copy-of select="."/> -->
94
	</xsl:template>
95

  
96
	<xsl:template match="TEI|teiCorpus|group|text">
97
		<xsl:copy>
98
			<xsl:apply-templates select="@*" />
99
			<xsl:apply-templates
100
				select="*|processing-instruction()|comment()|text()" />
101
		</xsl:copy>
102
	</xsl:template>
103

  
104

  
105
	<xsl:template match="text/body|text/back|text/front">
106
		<xsl:variable name="pages">
107
			<xsl:copy>
108
				<xsl:apply-templates select="@*" />
109
				<xsl:apply-templates
110
					select="*|processing-instruction()|comment()|text()" />
111
			</xsl:copy>
112
		</xsl:variable>
113
		<xsl:for-each select="$pages">
114
			<xsl:apply-templates mode="pass2" />
115
		</xsl:for-each>
116
	</xsl:template>
117

  
118

  
119
	<!-- first (recursive) pass. look for <pb> elements and group on them -->
120
	<xsl:template
121
		match="comment()|@*|processing-instruction()|text()">
122
		<xsl:copy-of select="." />
123
	</xsl:template>
124

  
125
	<xsl:template match="*">
126
		<xsl:call-template name="checkpb">
127
			<xsl:with-param name="eName" select="local-name()" />
128
		</xsl:call-template>
129
	</xsl:template>
130

  
131
	<xsl:template match="milestone[@unit='surface']">
132
		<xsl:copy-of select="." />
133
	</xsl:template>
134

  
135
	<xsl:template name="checkpb">
136
		<xsl:param name="eName" />
137
		<xsl:choose>
138
			<xsl:when test="not(.//milestone[@unit='surface'])">
139
				<xsl:copy-of select="." />
140
			</xsl:when>
141
			<xsl:otherwise>
142
				<xsl:variable name="pass">
143
					<xsl:call-template name="groupbypb">
144
						<xsl:with-param name="Name" select="$eName" />
145
					</xsl:call-template>
146
				</xsl:variable>
147
				<xsl:for-each select="$pass">
148
					<xsl:apply-templates />
149
				</xsl:for-each>
150
			</xsl:otherwise>
151
		</xsl:choose>
152
	</xsl:template>
153

  
154
	<xsl:template name="groupbypb">
155
		<xsl:param name="Name" />
156
		<xsl:for-each-group select="node()"
157
			group-starting-with="milestone[@unit='surface']">
158
			<xsl:choose>
159
				<xsl:when test="self::milestone[@unit='surface']">
160
					<xsl:copy-of select="." />
161
					<xsl:element name="{$Name}"
162
						namespace="http://www.tei-c.org/ns/1.0">
163
						<xsl:attribute name="rend">CONTINUED</xsl:attribute>
164
						<xsl:apply-templates
165
							select="current-group() except ." />
166
					</xsl:element>
167
				</xsl:when>
168
				<xsl:otherwise>
169
					<xsl:element name="{$Name}"
170
						namespace="http://www.tei-c.org/ns/1.0">
171
						<xsl:for-each select="..">
172
							<xsl:copy-of select="@*" />
173
							<xsl:apply-templates select="current-group()" />
174
						</xsl:for-each>
175
					</xsl:element>
176
				</xsl:otherwise>
177
			</xsl:choose>
178
		</xsl:for-each-group>
179
	</xsl:template>
180

  
181
	<!-- second pass. group by <pb> (now all at top level) and wrap groups in 
182
		<page> -->
183
	<xsl:template match="*" mode="pass2">
184
		<xsl:copy>
185
			<xsl:apply-templates
186
				select="@*|*|processing-instruction()|comment()|text()" mode="pass2" />
187
		</xsl:copy>
188
	</xsl:template>
189

  
190
	<xsl:template
191
		match="comment()|@*|processing-instruction()|text()" mode="pass2">
192
		<xsl:copy-of select="." />
193
	</xsl:template>
194

  
195

  
196
	<xsl:template match="*[milestone[@unit='surface']]"
197
		mode="pass2">
198
		<xsl:copy>
199
			<xsl:apply-templates select="@*" />
200
			<xsl:for-each-group select="*"
201
				group-starting-with="milestone[@unit='surface']">
202
				<xsl:choose>
203
					<xsl:when test="self::milestone[@unit='surface']">
204
						<xsl:comment>
205
							Page
206
							<xsl:value-of select="@xml:id" />
207
							enregistrée dans
208
							<xsl:value-of
209
								select="concat($output-directory,'/',$filename,'_',@xml:id,'.xml')" />
210
						</xsl:comment>
211
						<xsl:text>&#xA;</xsl:text>
212
						<xsl:result-document
213
							href="{$output-directory}/{$filename}_{@xml:id}.xml" indent="yes"
214
							exclude-result-prefixes="#all">
215
							<TEI xmlns="http://www.tei-c.org/ns/1.0">
216
								<teiHeader>
217
									<fileDesc>
218
										<titleStmt>
219
											<title>
220
												Page
221
												<xsl:value-of select="@xml:id" />
222
												of
223
												<xsl:value-of select="$filename" />
224
												tokenized at character level.
225
											</title>
226
										</titleStmt>
227
										<publicationStmt>
228
											<p>Oriflamms project (http://oriflamms.hypotheses.org)</p>
229
										</publicationStmt>
230
										<sourceDesc>
231
											<p>
232
												Converted from
233
												<xsl:value-of select="$filename" />
234
												.
235
											</p>
236
										</sourceDesc>
237
									</fileDesc>
238
									<revisionDesc>
239
										<change when="{current-dateTime()}">File created.</change>
240
									</revisionDesc>
241
								</teiHeader>
242
								<text>
243
									<body>
244
										<xsl:copy-of select="." />
245
										<xsl:copy-of select="current-group() except ." />
246
									</body>
247
								</text>
248
							</TEI>
249
						</xsl:result-document>
250
					</xsl:when>
251
					<xsl:otherwise>
252
						<xsl:copy-of select="current-group()" />
253
					</xsl:otherwise>
254
				</xsl:choose>
255
			</xsl:for-each-group>
256
		</xsl:copy>
257
	</xsl:template>
258

  
226 259
</xsl:stylesheet>

Also available in: Unified diff