3 SPDX-FileCopyrightText: 2023, 2024 Lady <https://www.ladys.computer/about/#lady>
4 SPDX-License-Identifier: MPL-2.0
7 ⁌ ⛩📰 书社 ∷ lib/catalog2transform.xslt
9 © 2023–2024 Lady [@ Ladys Computer].
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/>.
15 <!ENTITY 书社 "urn:fdc:ladys.computer:20231231:Shu1She4">
16 <!ENTITY xml "http://www.w3.org/XML/1998/namespace">
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"
35 <include href="literally.xslt"/>
36 <namespace-alias stylesheet-prefix="xslt" result-prefix="#default"/>
37 <param name="METADATA" select="'about:blank'"/>
40 <for-each select="//catalog:uri[@name]">
42 <value-of select="@name"/>
46 <variable name="unique-ids">
47 <for-each select="exsl:node-set($ids)/*">
48 <if test="not(preceding-sibling::*[string(.)=string(current())])">
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'"/>
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"/>
69 <xslt:variable name="书社:expansion" select="exsl:node-set($书社:expansion-fragment)"/>
70 <xslt:variable name="书社:result-fragment">
71 <xslt:apply-templates select="$书社:expansion/node()"/>
73 <xslt:variable name="书社:result" select="exsl:node-set($书社:result-fragment)"/>
74 <xslt:variable name="书社:destination">
76 <xslt:when test="string($书社:source/@书社:destination)!=''">
77 <xslt:value-of select="$书社:source/@书社:destination"/>
79 <xslt:when test="starts-with($PATH, '/')">
80 <xslt:value-of select="substring-after($PATH, '/')"/>
83 <xslt:value-of select="$PATH"/>
87 <for-each select="//catalog:uri">
88 <xslt:include href="{@uri}">
89 <attribute name="书社:id">
90 <value-of select="@name"/>
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="@*"/>
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">
107 <attribute name="local-name">
108 <text>{local-name()}</text>
110 <attribute name="name">
111 <text>{name()}</text>
113 <attribute name="namespace-uri">
114 <text>{namespace-uri()}</text>
119 <xslt:variable name="lang">
121 <xslt:when test="(self::html:*|self::svg:*)/@lang">
122 <xslt:value-of select="@lang"/>
124 <xslt:when test="@xml:lang">
125 <xslt:value-of select="@xml:lang"/>
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']"/>
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']"/>
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']"/>
139 <xslt:if test="$and-version and $THISREV">
140 <xslt:attribute name="书社:version">
141 <xslt:value-of select="$THISREV"/>
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"/>
150 <xslt:attribute name="xml:lang">
151 <xslt:value-of select="$lang"/>
154 <xslt:for-each select="exsl:node-set($attribute-names)/*">
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]"/>
163 <attribute name="name">
166 <attribute name="namespace">
167 <text>{@namespace-uri}</text>
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()">
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]">
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()">
194 <xslt:copy-of select="node()"/>
198 <xslt:copy-of select="."/>
203 <xslt:template name="书社:wrap">
204 <xslt:param name="nodes" select="/.."/>
205 <xslt:variable name="modalinput">
206 <xslt:copy-of select="$nodes"/>
207 <apply-templates select="exsl:node-set($unique-ids)" mode="书社:literally"/>
209 <xslt:variable name="metadata">
210 <xslt:copy-of select="$nodes[self::html:html]/html:head/node()|$nodes[self::html:head]/node()"/>
211 <xslt:apply-templates select="exsl:node-set($modalinput)/node()" mode="书社:metadata"/>
214 <xslt:copy-of select="$nodes[self::html:html]/@*"/>
215 <xslt:if test="not($nodes[self::html:html]/@书社:archived-as) and $nodes/@书社:archived-as">
216 <xslt:attribute name="书社:archived-as">
217 <xslt:value-of select="$nodes/@书社:archived-as"/>
220 <xslt:if test="not($nodes[self::html:html]/@lang) and $nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang">
221 <xslt:attribute name="lang">
222 <xslt:value-of select="$nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang"/>
225 <xslt:if test="not($nodes[self::html:html]/@xml:lang) and $nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang">
226 <xslt:attribute name="xml:lang">
227 <xslt:value-of select="$nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang"/>
231 <xslt:copy-of select="$nodes[self::html:html]/html:head/@*|$nodes[self::html:head]/@*"/>
233 <xslt:for-each select="exsl:node-set($metadata)/html:title">
234 <xslt:value-of select="."/>
235 <xslt:if test="position()!=last()">
242 <xslt:copy-of select="exsl:node-set($metadata)/node()[not(self::html:title)]"/>
243 <html:meta name="generator">
244 <xslt:attribute name="content">
245 <xslt:for-each select="exsl:node-set($metadata)/html:meta[@name='generator']">
246 <xslt:value-of select="@content"/>
254 <xslt:if test="$THISREV">
258 <xslt:value-of select="$THISREV"/>
267 <xslt:copy-of select="$nodes[self::html:html]/html:body/@*|$nodes[self::html:body]/@*"/>
268 <xslt:apply-templates select="exsl:node-set($modalinput)/node()" mode="书社:header"/>
269 <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()"/>
270 <xslt:apply-templates select="exsl:node-set($modalinput)/node()" mode="书社:footer"/>
274 <xslt:template match="/" priority="1">
275 <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)])]"/>
276 <xslt:variable name="root-with-attributes">
278 <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">
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="$result-nodes"/>
283 </xslt:call-template>
286 <xslt:variable name="wrapped-result">
287 <xslt:call-template name="书社:wrap">
288 <xslt:with-param name="nodes" select="$result-nodes"/>
289 </xslt:call-template>
291 <xslt:call-template name="书社:apply-attributes">
292 <xslt:with-param name="and-version" select="true()"/>
293 <xslt:with-param name="context-nodes" select="$书社:result//书社:apply-attributes-to-root"/>
294 <xslt:with-param name="destination-nodes" select="exsl:node-set($wrapped-result)/node()"/>
295 </xslt:call-template>
299 <xslt:apply-templates select="exsl:node-set($root-with-attributes)/node()" mode="书社:apply"/>
301 <xslt:template match="@*|node()" priority="-1">
303 <xslt:apply-templates select="@*|node()"/>
306 <xslt:template match="@*|node()" mode="书社:apply">
308 <xslt:apply-templates select="@*|node()" mode="书社:apply"/>
311 <xslt:template match="@书社:destination|@书社:disable-output-wrapping|@书社:archived-as[../ancestor::*[not(self::书社:apply-attributes-to-root or self::书社:apply-attributes)]]" mode="书社:apply" priority="1"/>
312 <xslt:template match="书社:archive" mode="书社:apply" priority="1">
314 <xslt:for-each select="@*">
316 <xslt:when test="local-name()='archived-as' and namespace-uri()='&书社;' and ../ancestor::*[not(self::书社:apply-attributes-to-root or self::书社:apply-attributes)]"/>
318 <xslt:apply-templates select="." mode="书社:apply"/>
322 <xslt:for-each select="node()">
324 <xslt:when test="self::*">
325 <xslt:variable name="component">
327 <xslt:when test="self::书社:*[local-name()='raw-text' or local-name()='base64-binary' or local-name()='archive']|@书社:disable-output-wrapping">
328 <xslt:copy-of select="."/>
331 <xslt:call-template name="书社:wrap">
332 <xslt:with-param name="nodes" select="."/>
333 </xslt:call-template>
337 <xslt:apply-templates select="exsl:node-set($component)/node()" mode="书社:apply"/>
340 <xslt:apply-templates select="." mode="书社:apply"/>
346 <xslt:template match="书社:apply-attributes" mode="书社:apply" priority="1">
347 <xslt:variable name="children">
348 <xslt:apply-templates mode="书社:apply"/>
350 <xslt:call-template name="书社:apply-attributes">
351 <xslt:with-param name="context-nodes" select="."/>
352 <xslt:with-param name="destination-nodes" select="exsl:node-set($children)/node()"/>
353 </xslt:call-template>
355 <xslt:template match="书社:apply-attributes-to-root" mode="书社:apply" priority="1">
356 <xslt:apply-templates mode="书社:apply"/>
358 <xslt:template match="书社:link[@xlink:show='embed']" mode="书社:expand" priority="1">
359 <xslt:variable name="identifier" select="string(@xlink:href)"/>
360 <xslt:variable name="uri" select="document($METADATA)//*[@rdf:about=$identifier]/dct:hasFormat/*[nie:interpretedAs/*[@nie:mimeType='application/xml']]/@nfo:fileUrl[1]"/>
362 <xslt:when test="$uri">
363 <xslt:variable name="expanded">
364 <xslt:apply-templates select="document($uri)" mode="书社:expand">
365 <xslt:with-param name="identifier" select="$identifier"/>
366 </xslt:apply-templates>
368 <xslt:for-each select="exsl:node-set($expanded)/node()">
371 <xslt:when test="self::html:*">
372 <xslt:attribute name="itemscope">itemscope</xslt:attribute>
373 <xslt:attribute name="itemtype">
375 <text>&书社;:embed</text>
377 <xslt:for-each select="exslstr:tokenize(@itemtype)/token[string()!='&书社;:document']">
381 <xslt:value-of select="."/>
384 <xslt:copy-of select="@*[not(namespace-uri()='' and (local-name()='itemscope' or local-name()='itemtype'))]|node()"/>
387 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
395 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
400 <xslt:template match="/node()" mode="书社:expand" priority="0">
401 <xslt:param name="identifier" select="$IDENTIFIER"/>
403 <xslt:if test="self::*">
404 <xslt:attribute name="书社:cksum">
405 <xslt:value-of select="$书社:about//*[@rdf:about=$identifier]/nfo:hasHash[@nfo:hashAlgorithm='CRC32']/@nfo:hashValue"/>
407 <xslt:attribute name="书社:identifier">
408 <xslt:value-of select="$identifier"/>
410 <xslt:attribute name="书社:mtime">
411 <xslt:value-of select="$书社:about//*[@rdf:about=$identifier]/nfo:fileLastModified"/>
415 <xslt:when test="self::html:*">
416 <xslt:attribute name="itemscope">itemscope</xslt:attribute>
417 <xslt:attribute name="itemtype">
419 <text>&书社;:document</text>
421 <xslt:for-each select="exslstr:tokenize(@itemtype)/token">
425 <xslt:value-of select="."/>
428 <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:apply-templates select="@*[not(namespace-uri()='&书社;' and (local-name()='cksum' or local-name()='identifier' or local-name()='mtime'))]|node()" mode="书社:expand"/>
436 <xslt:template match="@*|node()[not(self::书社:link) or not(@xlink:show='embed')]" mode="书社:expand" priority="-1">
438 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
441 <xslt:template match="text()" mode="书社:header"/>
442 <xslt:template match="text()" mode="书社:footer"/>
443 <xslt:template match="text()" mode="书社:metadata"/>
444 <xslt:output method="xml" encoding="UTF-8" cdata-section-elements="html:script html:style html:textarea 书社:raw-output 书社:raw-text"/>
447 <output method="xml" encoding="UTF-8"/>