]> Lady’s Gitweb - Shushe/blob - lib/catalog2transform.xslt
5098a2cdd38b92d5cc61f587e680cf3a009ad9ee
[Shushe] / lib / catalog2transform.xslt
1 <?xml version="1.0"?>
2 <!--
3 SPDX-FileCopyrightText: 2023, 2024 Lady <https://www.ladys.computer/about/#lady>
4 SPDX-License-Identifier: MPL-2.0
5 -->
6 <!--
7 ⁌ ⛩📰 书社 ∷ lib/catalog2transform.xslt
8
9 © 2023–2024 Lady [@ Ladys Computer].
10
11 This Source Code Form is subject to the terms of the Mozilla Public License, v 2.0.
12 If a copy of the M·P·L was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
13 -->
14 <!DOCTYPE transform [
15 <!ENTITY 书社 "urn:fdc:ladys.computer:20231231:Shu1She4">
16 <!ENTITY xml "http://www.w3.org/XML/1998/namespace">
17 ]>
18 <transform
19 xmlns="http://www.w3.org/1999/XSL/Transform"
20 xmlns:dct="http://purl.org/dc/terms/"
21 xmlns:catalog="urn:oasis:names:tc:entity:xmlns:xml:catalog"
22 xmlns:exsl="http://exslt.org/common"
23 xmlns:exslstr="http://exslt.org/strings"
24 xmlns:html="http://www.w3.org/1999/xhtml"
25 xmlns:nie="http://www.semanticdesktop.org/ontologies/2007/01/19/nie#"
26 xmlns:nfo="http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#"
27 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
28 xmlns:svg="http://www.w3.org/2000/svg"
29 xmlns:xlink="http://www.w3.org/1999/xlink"
30 xmlns:xslt="http://www.w3.org/1999/XSL/TransformAlias"
31 xmlns:书社="urn:fdc:ladys.computer:20231231:Shu1She4"
32 exclude-result-prefixes="catalog"
33 version="1.0"
34 >
35 <include href="literally.xslt"/>
36 <namespace-alias stylesheet-prefix="xslt" result-prefix="#default"/>
37 <param name="METADATA" select="'about:blank'"/>
38 <template match="/">
39 <variable name="ids">
40 <for-each select="//catalog:uri[@name]">
41 <书社:id>
42 <value-of select="@name"/>
43 </书社:id>
44 </for-each>
45 </variable>
46 <variable name="unique-ids">
47 <for-each select="exsl:node-set($ids)/*">
48 <if test="not(preceding-sibling::*[string(.)=string(current())])">
49 <copy-of select="."/>
50 </if>
51 </for-each>
52 </variable>
53 <xslt:transform exclude-result-prefixes="nie nfo" extension-element-prefixes="exsl exslstr" version="1.0">
54 <xslt:param name="BUILDTIME" select="'1972-12-31T00:00:00Z'"/>
55 <xslt:param name="IDENTIFIER" select="false()"/>
56 <xslt:param name="PATH" select="'/unknown'"/>
57 <xslt:param name="SRCREV" select="false()"/>
58 <xslt:param name="THISREV" select="false()"/>
59 <xslt:variable name="书社:source" select="/"/>
60 <xslt:variable name="书社:about-fragment">
61 <apply-templates select="document($METADATA)" mode="书社:literally">
62 <with-param name="extension-element-namespaces" select="'http://exslt.org/common http://exslt.org/strings'"/>
63 </apply-templates>
64 </xslt:variable>
65 <xslt:variable name="书社:about" select="exsl:node-set($书社:about-fragment)"/>
66 <xslt:variable name="书社:expansion-fragment">
67 <xslt:apply-templates select="$书社:source/node()" mode="书社:expand"/>
68 </xslt:variable>
69 <xslt:variable name="书社:expansion" select="exsl:node-set($书社:expansion-fragment)"/>
70 <xslt:variable name="书社:result-fragment">
71 <xslt:apply-templates select="$书社:expansion/node()"/>
72 </xslt:variable>
73 <xslt:variable name="书社:result" select="exsl:node-set($书社:result-fragment)"/>
74 <xslt:variable name="书社:destination">
75 <xslt:choose>
76 <xslt:when test="string($书社:source/@书社:destination)!=''">
77 <xslt:value-of select="$书社:source/@书社:destination"/>
78 </xslt:when>
79 <xslt:when test="starts-with($PATH, '/')">
80 <xslt:value-of select="substring-after($PATH, '/')"/>
81 </xslt:when>
82 <xslt:otherwise>
83 <xslt:value-of select="$PATH"/>
84 </xslt:otherwise>
85 </xslt:choose>
86 </xslt:variable>
87 <for-each select="//catalog:uri">
88 <xslt:include href="{@uri}">
89 <attribute name="书社:id">
90 <value-of select="@name"/>
91 </attribute>
92 </xslt:include>
93 </for-each>
94 <xslt:template name="书社:apply-attributes">
95 <xslt:param name="and-version" select="false()"/>
96 <xslt:param name="context-nodes" select="/.."/>
97 <xslt:param name="destination-nodes" select="/.."/>
98 <xslt:variable name="additional-attributes" select="$context-nodes/@*"/>
99 <xslt:for-each select="$destination-nodes">
100 <xslt:variable name="existing-attributes" select="@*"/>
101 <xslt:choose>
102 <xslt:when test="self::*">
103 <xslt:variable name="context" select="."/>
104 <xslt:variable name="attribute-names">
105 <xslt:for-each select="$existing-attributes|$additional-attributes">
106 <书社:attribute>
107 <attribute name="local-name">
108 <text>{local-name()}</text>
109 </attribute>
110 <attribute name="name">
111 <text>{name()}</text>
112 </attribute>
113 <attribute name="namespace-uri">
114 <text>{namespace-uri()}</text>
115 </attribute>
116 </书社:attribute>
117 </xslt:for-each>
118 </xslt:variable>
119 <xslt:variable name="lang">
120 <xslt:choose>
121 <xslt:when test="(self::html:*|self::svg:*)/@lang">
122 <xslt:value-of select="@lang"/>
123 </xslt:when>
124 <xslt:when test="@xml:lang">
125 <xslt:value-of select="@xml:lang"/>
126 </xslt:when>
127 <xslt:when test="$additional-attributes[(parent::html:* or parent::svg:*) and namespace-uri()='' and local-name()='lang']">
128 <xslt:value-of select="$additional-attributes[(parent::html:* or parent::svg:*) and namespace-uri()='' and local-name()='lang']"/>
129 </xslt:when>
130 <xslt:when test="(self::html:* or self::svg:*) and $additional-attributes[(parent::书社:apply-attributes or parent::书社:apply-attributes-to-root) and namespace-uri()='' and local-name()='lang']">
131 <xslt:value-of select="$additional-attributes[(parent::书社:apply-attributes or parent::书社:apply-attributes-to-root) and namespace-uri()='' and local-name()='lang']"/>
132 </xslt:when>
133 <xslt:when test="$additional-attributes[namespace-uri()='&xml;' and local-name()='lang']">
134 <xslt:value-of select="$additional-attributes[namespace-uri()='&xml;' and local-name()='lang']"/>
135 </xslt:when>
136 </xslt:choose>
137 </xslt:variable>
138 <xslt:copy>
139 <xslt:if test="$and-version and $THISREV">
140 <xslt:attribute name="书社:version">
141 <xslt:value-of select="$THISREV"/>
142 </xslt:attribute>
143 </xslt:if>
144 <xslt:if test="string($lang)!=''">
145 <xslt:if test="self::html:* or self::svg:*">
146 <xslt:attribute name="lang">
147 <xslt:value-of select="$lang"/>
148 </xslt:attribute>
149 </xslt:if>
150 <xslt:attribute name="xml:lang">
151 <xslt:value-of select="$lang"/>
152 </xslt:attribute>
153 </xslt:if>
154 <xslt:for-each select="exsl:node-set($attribute-names)/*">
155 <xslt:choose>
156 <xslt:when test="@namespace-uri='&xml;'"/>
157 <xslt:when test="$context[self::html:* or self::svg:*] and @namespace-uri='' and @local-name='lang'"/>
158 <xslt:when test="@namespace-uri='&书社;' and @local-name='archived-as' and not($context-nodes/ancestor::*[not(self::书社:apply-attributes-to-root or self::书社:apply-attributes)][1][self::书社:archive])"/>
159 <xslt:when test="@namespace-uri='&书社;' and (@local-name='destination' or @local-name='disable-output-wrapping' or @local-name='version')"/>
160 <xslt:when test="preceding-sibling::*[@local-name=current()/@local-name and @namespace-uri=current()/@namespace-uri]"/>
161 <xslt:otherwise>
162 <xslt:attribute>
163 <attribute name="name">
164 <text>{@name}</text>
165 </attribute>
166 <attribute name="namespace">
167 <text>{@namespace-uri}</text>
168 </attribute>
169 <xslt:for-each select="$existing-attributes[local-name()=current()/@local-name and namespace-uri()=current()/@namespace-uri]">
170 <xslt:value-of select="."/>
171 <xslt:if test="position()!=last()">
172 <xslt:text>
173 <text> </text>
174 </xslt:text>
175 </xslt:if>
176 </xslt:for-each>
177 <xslt:if test="$existing-attributes[local-name()=current()/@local-name and namespace-uri()=current()/@namespace-uri] and $additional-attributes[local-name()=current()/@local-name and namespace-uri()=current()/@namespace-uri]">
178 <xslt:text>
179 <text> </text>
180 </xslt:text>
181 </xslt:if>
182 <xslt:for-each select="$additional-attributes[local-name()=current()/@local-name and namespace-uri()=current()/@namespace-uri]">
183 <xslt:value-of select="."/>
184 <xslt:if test="position()!=last()">
185 <xslt:text>
186 <text> </text>
187 </xslt:text>
188 </xslt:if>
189 </xslt:for-each>
190 </xslt:attribute>
191 </xslt:otherwise>
192 </xslt:choose>
193 </xslt:for-each>
194 <xslt:copy-of select="node()"/>
195 </xslt:copy>
196 </xslt:when>
197 <xslt:otherwise>
198 <xslt:copy-of select="."/>
199 </xslt:otherwise>
200 </xslt:choose>
201 </xslt:for-each>
202 </xslt:template>
203 <xslt:template name="书社:wrap">
204 <xslt:param name="nodes" select="/.."/>
205 <xslt:variable name="modalinput-fragment">
206 <xslt:copy-of select="$nodes"/>
207 <apply-templates select="exsl:node-set($unique-ids)" mode="书社:literally"/>
208 </xslt:variable>
209 <xslt:variable name="modalinput" select="exsl:node-set($modalinput-fragment)"/>
210 <xslt:variable name="metadata-fragment">
211 <xslt:copy-of select="$nodes[self::html:html]/html:head/node()|$nodes[self::html:head]/node()"/>
212 <xslt:apply-templates select="$modalinput/node()" mode="书社:metadata"/>
213 </xslt:variable>
214 <xslt:variable name="metadata" select="exsl:node-set($metadata-fragment)"/>
215 <html:html>
216 <xslt:copy-of select="$nodes[self::html:html]/@*"/>
217 <xslt:if test="not($nodes[self::html:html]/@书社:archived-as) and $nodes/@书社:archived-as">
218 <xslt:attribute name="书社:archived-as">
219 <xslt:value-of select="$nodes/@书社:archived-as"/>
220 </xslt:attribute>
221 </xslt:if>
222 <xslt:if test="not($nodes[self::html:html]/@lang) and $nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang">
223 <xslt:attribute name="lang">
224 <xslt:value-of select="$nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang"/>
225 </xslt:attribute>
226 </xslt:if>
227 <xslt:if test="not($nodes[self::html:html]/@xml:lang) and $nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang">
228 <xslt:attribute name="xml:lang">
229 <xslt:value-of select="$nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang"/>
230 </xslt:attribute>
231 </xslt:if>
232 <html:head>
233 <xslt:copy-of select="$nodes[self::html:html]/html:head/@*|$nodes[self::html:head]/@*"/>
234 <html:title>
235 <xslt:for-each select="$metadata/html:title">
236 <xslt:value-of select="."/>
237 <xslt:if test="position()!=last()">
238 <xslt:text>
239 <text> </text>
240 </xslt:text>
241 </xslt:if>
242 </xslt:for-each>
243 </html:title>
244 <xslt:copy-of select="$metadata/node()[not(self::html:title or self::html:meta and @name='generator')]"/>
245 <html:meta name="generator">
246 <xslt:attribute name="content">
247 <xslt:for-each select="$metadata/html:meta[@name='generator']">
248 <xslt:value-of select="@content"/>
249 <xslt:text>
250 <text>, </text>
251 </xslt:text>
252 </xslt:for-each>
253 <xslt:text>
254 <text>⛩📰 书社</text>
255 </xslt:text>
256 <xslt:if test="$THISREV">
257 <xslt:text>
258 <text> (</text>
259 </xslt:text>
260 <xslt:value-of select="$THISREV"/>
261 <xslt:text>
262 <text>)</text>
263 </xslt:text>
264 </xslt:if>
265 </xslt:attribute>
266 </html:meta>
267 </html:head>
268 <html:body>
269 <xslt:copy-of select="$nodes[self::html:html]/html:body/@*|$nodes[self::html:body]/@*"/>
270 <xslt:apply-templates select="$modalinput/node()" mode="书社:header"/>
271 <xslt:copy-of select="$nodes[not(self::html:html or self::html:head or self::html:body)]|$nodes[self::html:html]/node()[not(self::html:head or self::html:body)]|$nodes[self::html:html]/html:body/node()|$nodes[self::html:body]/node()"/>
272 <xslt:apply-templates select="$modalinput/node()" mode="书社:footer"/>
273 </html:body>
274 </html:html>
275 </xslt:template>
276 <xslt:template match="/" priority="1">
277 <xslt:variable name="result-nodes" select="$书社:result/node()[not(self::书社:apply-attributes-to-root)]|$书社:result/书社:apply-attributes-to-root/descendant::node()[not(self::书社:apply-attributes-to-root) and not(ancestor::*[not(self::书社:apply-attributes-to-root)])]"/>
278 <xslt:variable name="root-with-attributes">
279 <xslt:choose>
280 <xslt:when test="not($result-nodes/self::html:*) or $result-nodes/self::书社:*[local-name()='raw-text' or local-name()='base64-binary' or local-name()='archive'] or $result-nodes/@书社:disable-output-wrapping or $书社:result//书社:apply-attributes-to-root/@书社:disable-output-wrapping">
281 <xslt:call-template name="书社:apply-attributes">
282 <xslt:with-param name="and-version" select="true()"/>
283 <xslt:with-param name="context-nodes" select="$书社:result//书社:apply-attributes-to-root"/>
284 <xslt:with-param name="destination-nodes" select="$result-nodes"/>
285 </xslt:call-template>
286 </xslt:when>
287 <xslt:otherwise>
288 <xslt:variable name="wrapped-result">
289 <xslt:call-template name="书社:wrap">
290 <xslt:with-param name="nodes" select="$result-nodes"/>
291 </xslt:call-template>
292 </xslt:variable>
293 <xslt:call-template name="书社:apply-attributes">
294 <xslt:with-param name="and-version" select="true()"/>
295 <xslt:with-param name="context-nodes" select="$书社:result//书社:apply-attributes-to-root"/>
296 <xslt:with-param name="destination-nodes" select="exsl:node-set($wrapped-result)/node()"/>
297 </xslt:call-template>
298 </xslt:otherwise>
299 </xslt:choose>
300 </xslt:variable>
301 <xslt:apply-templates select="exsl:node-set($root-with-attributes)/node()" mode="书社:apply"/>
302 </xslt:template>
303 <xslt:template match="@*|node()" priority="-1">
304 <xslt:copy>
305 <xslt:apply-templates select="@*|node()"/>
306 </xslt:copy>
307 </xslt:template>
308 <xslt:template match="@*|node()" mode="书社:apply">
309 <xslt:copy>
310 <xslt:apply-templates select="@*|node()" mode="书社:apply"/>
311 </xslt:copy>
312 </xslt:template>
313 <xslt:template match="@书社:destination|@书社:disable-output-wrapping|@书社:archived-as[../ancestor::*[not(self::书社:apply-attributes-to-root or self::书社:apply-attributes)]]" mode="书社:apply" priority="1"/>
314 <xslt:template match="书社:archive" mode="书社:apply" priority="1">
315 <xslt:copy>
316 <xslt:for-each select="@*">
317 <xslt:choose>
318 <xslt:when test="local-name()='archived-as' and namespace-uri()='&书社;' and ../ancestor::*[not(self::书社:apply-attributes-to-root or self::书社:apply-attributes)]"/>
319 <xslt:otherwise>
320 <xslt:apply-templates select="." mode="书社:apply"/>
321 </xslt:otherwise>
322 </xslt:choose>
323 </xslt:for-each>
324 <xslt:for-each select="node()">
325 <xslt:choose>
326 <xslt:when test="self::*">
327 <xslt:variable name="component">
328 <xslt:choose>
329 <xslt:when test="self::书社:*[local-name()='raw-text' or local-name()='base64-binary' or local-name()='archive']|@书社:disable-output-wrapping">
330 <xslt:copy-of select="."/>
331 </xslt:when>
332 <xslt:otherwise>
333 <xslt:call-template name="书社:wrap">
334 <xslt:with-param name="nodes" select="."/>
335 </xslt:call-template>
336 </xslt:otherwise>
337 </xslt:choose>
338 </xslt:variable>
339 <xslt:apply-templates select="exsl:node-set($component)/node()" mode="书社:apply"/>
340 </xslt:when>
341 <xslt:otherwise>
342 <xslt:apply-templates select="." mode="书社:apply"/>
343 </xslt:otherwise>
344 </xslt:choose>
345 </xslt:for-each>
346 </xslt:copy>
347 </xslt:template>
348 <xslt:template match="书社:apply-attributes" mode="书社:apply" priority="1">
349 <xslt:variable name="children">
350 <xslt:apply-templates mode="书社:apply"/>
351 </xslt:variable>
352 <xslt:call-template name="书社:apply-attributes">
353 <xslt:with-param name="context-nodes" select="."/>
354 <xslt:with-param name="destination-nodes" select="exsl:node-set($children)/node()"/>
355 </xslt:call-template>
356 </xslt:template>
357 <xslt:template match="书社:apply-attributes-to-root" mode="书社:apply" priority="1">
358 <xslt:apply-templates mode="书社:apply"/>
359 </xslt:template>
360 <xslt:template match="书社:link[@xlink:show='embed']" mode="书社:expand" priority="1">
361 <xslt:variable name="identifier" select="string(@xlink:href)"/>
362 <xslt:variable name="uri" select="document($METADATA)//*[@rdf:about=$identifier]/dct:hasFormat/*[nie:interpretedAs/*[@nie:mimeType='application/xml']]/@nfo:fileUrl[1]"/>
363 <xslt:choose>
364 <xslt:when test="$uri">
365 <xslt:variable name="expanded">
366 <xslt:apply-templates select="document($uri)" mode="书社:expand">
367 <xslt:with-param name="identifier" select="$identifier"/>
368 </xslt:apply-templates>
369 </xslt:variable>
370 <xslt:for-each select="exsl:node-set($expanded)/node()">
371 <xslt:copy>
372 <xslt:choose>
373 <xslt:when test="self::html:*">
374 <xslt:attribute name="itemscope">itemscope</xslt:attribute>
375 <xslt:attribute name="itemtype">
376 <xslt:text>
377 <text>&书社;:embed</text>
378 </xslt:text>
379 <xslt:for-each select="exslstr:tokenize(@itemtype)/token[string()!='&书社;:document']">
380 <xslt:text>
381 <text> </text>
382 </xslt:text>
383 <xslt:value-of select="."/>
384 </xslt:for-each>
385 </xslt:attribute>
386 <xslt:copy-of select="@*[not(namespace-uri()='' and (local-name()='itemscope' or local-name()='itemtype'))]|node()"/>
387 </xslt:when>
388 <xslt:otherwise>
389 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
390 </xslt:otherwise>
391 </xslt:choose>
392 </xslt:copy>
393 </xslt:for-each>
394 </xslt:when>
395 <xslt:otherwise>
396 <xslt:copy>
397 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
398 </xslt:copy>
399 </xslt:otherwise>
400 </xslt:choose>
401 </xslt:template>
402 <xslt:template match="/node()" mode="书社:expand" priority="0">
403 <xslt:param name="identifier" select="$IDENTIFIER"/>
404 <xslt:copy>
405 <xslt:if test="self::*">
406 <xslt:attribute name="书社:cksum">
407 <xslt:value-of select="$书社:about//*[@rdf:about=$identifier]/nfo:hasHash[@nfo:hashAlgorithm='CRC32']/@nfo:hashValue"/>
408 </xslt:attribute>
409 <xslt:attribute name="书社:identifier">
410 <xslt:value-of select="$identifier"/>
411 </xslt:attribute>
412 <xslt:attribute name="书社:mtime">
413 <xslt:value-of select="$书社:about//*[@rdf:about=$identifier]/nfo:fileLastModified"/>
414 </xslt:attribute>
415 </xslt:if>
416 <xslt:choose>
417 <xslt:when test="self::html:*">
418 <xslt:attribute name="itemscope">itemscope</xslt:attribute>
419 <xslt:attribute name="itemtype">
420 <xslt:text>
421 <text>&书社;:document</text>
422 </xslt:text>
423 <xslt:for-each select="exslstr:tokenize(@itemtype)/token">
424 <xslt:text>
425 <text> </text>
426 </xslt:text>
427 <xslt:value-of select="."/>
428 </xslt:for-each>
429 </xslt:attribute>
430 <xslt:apply-templates select="@*[not(namespace-uri()='' and (local-name()='itemscope' or local-name()='itemtype') or namespace-uri()='&书社;' and (local-name()='cksum' or local-name()='identifier' or local-name()='mtime'))]|node()" mode="书社:expand"/>
431 </xslt:when>
432 <xslt:otherwise>
433 <xslt:apply-templates select="@*[not(namespace-uri()='&书社;' and (local-name()='cksum' or local-name()='identifier' or local-name()='mtime'))]|node()" mode="书社:expand"/>
434 </xslt:otherwise>
435 </xslt:choose>
436 </xslt:copy>
437 </xslt:template>
438 <xslt:template match="@*|node()[not(self::书社:link) or not(@xlink:show='embed')]" mode="书社:expand" priority="-1">
439 <xslt:copy>
440 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
441 </xslt:copy>
442 </xslt:template>
443 <xslt:template match="text()" mode="书社:header"/>
444 <xslt:template match="text()" mode="书社:footer"/>
445 <xslt:template match="text()" mode="书社:metadata"/>
446 <xslt:output method="xml" encoding="UTF-8" cdata-section-elements="html:script html:style html:textarea 书社:raw-output 书社:raw-text"/>
447 </xslt:transform>
448 </template>
449 <output method="xml" encoding="UTF-8"/>
450 </transform>
This page took 0.122718 seconds and 3 git commands to generate.