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