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