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 [@ Lady’s 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 <namespace-alias stylesheet-prefix="xslt" result-prefix="#default"/>
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="IDENTIFIER" select="false()"/>
40 <xslt:param name="METADATA" select="'metadata'"/>
41 <xslt:param name="PATH" select="'/unknown'"/>
42 <xslt:param name="SRCREV" select="false()"/>
43 <xslt:param name="THISREV" select="false()"/>
44 <xslt:variable name="书社:source" select="/"/>
45 <xslt:variable name="书社:expansion">
46 <xslt:apply-templates select="$书社:source/node()" mode="书社:expand"/>
48 <xslt:variable name="书社:result">
49 <xslt:apply-templates select="exsl:node-set($书社:expansion)/node()"/>
51 <xslt:variable name="书社:destination">
53 <xslt:when test="string($书社:source/@书社:destination)!=''">
54 <xslt:value-of select="$书社:source/@书社:destination"/>
56 <xslt:when test="starts-with($PATH, '/')">
57 <xslt:value-of select="substring-after($PATH, '/')"/>
60 <xslt:value-of select="$PATH"/>
64 <for-each select="//catalog:uri">
65 <xslt:include href="{@uri}">
66 <if test="contains(@name, ':')">
67 <attribute name="书社:id">
68 <value-of select="@name"/>
73 <xslt:template name="书社:apply-attributes">
74 <xslt:param name="and-version" select="false()"/>
75 <xslt:param name="context-nodes" select="/.."/>
76 <xslt:param name="destination-nodes" select="/.."/>
77 <xslt:variable name="additional-attributes" select="$context-nodes/@*"/>
78 <xslt:for-each select="$destination-nodes">
79 <xslt:variable name="existing-attributes" select="@*"/>
81 <xslt:when test="self::*">
82 <xslt:variable name="context" select="."/>
83 <xslt:variable name="attribute-names">
84 <xslt:for-each select="$existing-attributes|$additional-attributes">
86 <attribute name="local-name">
87 <text>{local-name()}</text>
89 <attribute name="name">
92 <attribute name="namespace-uri">
93 <text>{namespace-uri()}</text>
98 <xslt:variable name="lang">
100 <xslt:when test="(self::html:*|self::svg:*)/@lang">
101 <xslt:value-of select="@lang"/>
103 <xslt:when test="@xml:lang">
104 <xslt:value-of select="@xml:lang"/>
106 <xslt:when test="$additional-attributes[(parent::html:* or parent::svg:*) and namespace-uri()='' and local-name()='lang']">
107 <xslt:value-of select="$additional-attributes[(parent::html:* or parent::svg:*) and namespace-uri()='' and local-name()='lang']"/>
109 <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']">
110 <xslt:value-of select="$additional-attributes[(parent::书社:apply-attributes or parent::书社:apply-attributes-to-root) and namespace-uri()='' and local-name()='lang']"/>
112 <xslt:when test="$additional-attributes[namespace-uri()='&xml;' and local-name()='lang']">
113 <xslt:value-of select="$additional-attributes[namespace-uri()='&xml;' and local-name()='lang']"/>
118 <xslt:if test="$and-version and $THISREV">
119 <xslt:attribute name="书社:version">
120 <xslt:value-of select="$THISREV"/>
123 <xslt:if test="string($lang)!=''">
124 <xslt:if test="self::html:* or self::svg:*">
125 <xslt:attribute name="lang">
126 <xslt:value-of select="$lang"/>
129 <xslt:attribute name="xml:lang">
130 <xslt:value-of select="$lang"/>
133 <xslt:for-each select="exsl:node-set($attribute-names)/*">
135 <xslt:when test="@namespace-uri='&xml;'"/>
136 <xslt:when test="$context[self::html:* or self::svg:*] and @namespace-uri='' and @local-name='lang'"/>
137 <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])"/>
138 <xslt:when test="@namespace-uri='&书社;' and (@local-name='destination' or @local-name='disable-output-wrapping' or @local-name='version')"/>
139 <xslt:when test="preceding-sibling::*[@local-name=current()/@local-name and @namespace-uri=current()/@namespace-uri]"/>
142 <attribute name="name">
145 <attribute name="namespace">
146 <text>{@namespace-uri}</text>
148 <xslt:for-each select="$existing-attributes[local-name()=current()/@local-name and namespace-uri()=current()/@namespace-uri]">
149 <xslt:value-of select="."/>
150 <xslt:if test="position()!=last()">
156 <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]">
161 <xslt:for-each select="$additional-attributes[local-name()=current()/@local-name and namespace-uri()=current()/@namespace-uri]">
162 <xslt:value-of select="."/>
163 <xslt:if test="position()!=last()">
173 <xslt:copy-of select="node()"/>
177 <xslt:copy-of select="."/>
182 <xslt:template name="书社:wrap">
183 <xslt:param name="nodes" select="/.."/>
184 <xslt:variable name="modalinput-fragment">
185 <xslt:copy-of select="$nodes"/>
186 <xslt:copy-of select="document('')/xslt:transform/xslt:include"/>
188 <xslt:variable name="modalinput" select="exsl:node-set($modalinput-fragment)"/>
189 <xslt:variable name="metadata-fragment">
190 <xslt:copy-of select="$nodes[self::html:html]/html:head/node()|$nodes[self::html:head]/node()"/>
191 <xslt:apply-templates select="$modalinput/node()" mode="书社:metadata"/>
193 <xslt:variable name="metadata" select="exsl:node-set($metadata-fragment)"/>
195 <xslt:copy-of select="$nodes[self::html:html]/@*"/>
196 <xslt:if test="not($nodes[self::html:html]/@书社:archived-as) and $nodes/@书社:archived-as">
197 <xslt:attribute name="书社:archived-as">
198 <xslt:value-of select="$nodes/@书社:archived-as"/>
201 <xslt:if test="not($nodes[self::html:html]/@lang) and $nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang">
202 <xslt:attribute name="lang">
203 <xslt:value-of select="$nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang"/>
206 <xslt:if test="not($nodes[self::html:html]/@xml:lang) and $nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang">
207 <xslt:attribute name="xml:lang">
208 <xslt:value-of select="$nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang"/>
212 <xslt:copy-of select="$nodes[self::html:html]/html:head/@*|$nodes[self::html:head]/@*"/>
214 <xslt:for-each select="$metadata/html:title">
215 <xslt:value-of select="."/>
216 <xslt:if test="position()!=last()">
223 <xslt:copy-of select="$metadata/node()[not(self::html:title or self::html:meta and @name='generator')]"/>
224 <html:meta name="generator">
225 <xslt:attribute name="content">
226 <xslt:for-each select="$metadata/html:meta[@name='generator']">
227 <xslt:value-of select="@content"/>
235 <xslt:if test="$THISREV">
239 <xslt:value-of select="$THISREV"/>
248 <xslt:copy-of select="$nodes[self::html:html]/html:body/@*|$nodes[self::html:body]/@*"/>
249 <xslt:apply-templates select="$modalinput/node()" mode="书社:header"/>
250 <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()"/>
251 <xslt:apply-templates select="$modalinput/node()" mode="书社:footer"/>
255 <xslt:template match="/" priority="1">
256 <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)])]"/>
257 <xslt:variable name="root-with-attributes">
259 <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">
260 <xslt:call-template name="书社:apply-attributes">
261 <xslt:with-param name="and-version" select="true()"/>
262 <xslt:with-param name="context-nodes" select="exsl:node-set($书社:result)//书社:apply-attributes-to-root"/>
263 <xslt:with-param name="destination-nodes" select="$result-nodes"/>
264 </xslt:call-template>
267 <xslt:variable name="wrapped-result">
268 <xslt:call-template name="书社:wrap">
269 <xslt:with-param name="nodes" select="$result-nodes"/>
270 </xslt:call-template>
272 <xslt:call-template name="书社:apply-attributes">
273 <xslt:with-param name="and-version" select="true()"/>
274 <xslt:with-param name="context-nodes" select="exsl:node-set($书社:result)//书社:apply-attributes-to-root"/>
275 <xslt:with-param name="destination-nodes" select="exsl:node-set($wrapped-result)/node()"/>
276 </xslt:call-template>
280 <xslt:apply-templates select="exsl:node-set($root-with-attributes)/node()" mode="书社:application"/>
282 <xslt:template match="@*|node()" priority="-1">
284 <xslt:apply-templates select="@*|node()"/>
287 <xslt:template match="@*|node()" mode="书社:application">
289 <xslt:apply-templates select="@*|node()" mode="书社:application"/>
292 <xslt:template match="@书社:destination|@书社:disable-output-wrapping|@书社:archived-as[../ancestor::*[not(self::书社:apply-attributes-to-root or self::书社:apply-attributes)]]" mode="书社:application" priority="1"/>
293 <xslt:template match="书社:archive" mode="书社:application" priority="1">
295 <xslt:for-each select="@*">
297 <xslt:when test="local-name()='archived-as' and namespace-uri()='&书社;' and ../ancestor::*[not(self::书社:apply-attributes-to-root or self::书社:apply-attributes)]"/>
299 <xslt:apply-templates select="." mode="书社:application"/>
303 <xslt:for-each select="node()">
305 <xslt:when test="self::*">
306 <xslt:variable name="component">
308 <xslt:when test="self::书社:*[local-name()='raw-text' or local-name()='base64-binary' or local-name()='archive']|@书社:disable-output-wrapping">
309 <xslt:copy-of select="."/>
312 <xslt:call-template name="书社:wrap">
313 <xslt:with-param name="nodes" select="."/>
314 </xslt:call-template>
318 <xslt:apply-templates select="exsl:node-set($component)/node()" mode="书社:application"/>
321 <xslt:apply-templates select="." mode="书社:application"/>
327 <xslt:template match="书社:apply-attributes" mode="书社:application" priority="1">
328 <xslt:variable name="children">
329 <xslt:apply-templates mode="书社:application"/>
331 <xslt:call-template name="书社:apply-attributes">
332 <xslt:with-param name="context-nodes" select="."/>
333 <xslt:with-param name="destination-nodes" select="exsl:node-set($children)/node()"/>
334 </xslt:call-template>
336 <xslt:template match="书社:apply-attributes-to-root" mode="书社:application" priority="1">
337 <xslt:apply-templates mode="书社:application"/>
339 <xslt:template match="/node()" mode="书社:expand" priority="0">
342 <xslt:when test="self::html:*">
343 <xslt:attribute name="itemscope">itemscope</xslt:attribute>
344 <xslt:attribute name="itemtype">
346 <text>&书社;:document</text>
348 <xslt:for-each select="exslstr:tokenize(@itemtype)/token">
352 <xslt:value-of select="."/>
355 <xslt:apply-templates select="@*[not(namespace-uri()='' and (local-name()='itemscope' or local-name()='itemtype'))]|node()" mode="书社:expand"/>
358 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
363 <xslt:template match="书社:link[@xlink:show='embed']" mode="书社:expand" priority="1">
364 <xslt:variable name="identifier" select="string(@xlink:href)"/>
365 <xslt:variable name="uri" select="document($METADATA)//*[@rdf:about=$identifier]/dct:hasFormat/*[nie:interpretedAs/*[@nie:mimeType='application/xml']]/@nfo:fileUrl[1]"/>
367 <xslt:when test="$uri">
368 <xslt:variable name="expanded">
369 <xslt:apply-templates select="document($uri)" mode="书社:expand"/>
371 <xslt:for-each select="exsl:node-set($expanded)/node()">
374 <xslt:when test="self::html:*">
375 <xslt:attribute name="itemscope">itemscope</xslt:attribute>
376 <xslt:attribute name="itemtype">
378 <text>&书社;:embed</text>
380 <xslt:for-each select="exslstr:tokenize(@itemtype)/token[string()!='&书社;:document']">
384 <xslt:value-of select="."/>
387 <xslt:copy-of select="@*[not(namespace-uri()='' and (local-name()='itemscope' or local-name()='itemtype'))]|node()"/>
390 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
398 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
403 <xslt:template match="@*|node()[not(self::书社:link) or not(@xlink:show='embed')]" mode="书社:expand" priority="-1">
405 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
408 <xslt:template match="text()" mode="书社:header"/>
409 <xslt:template match="text()" mode="书社:footer"/>
410 <xslt:template match="text()" mode="书社:metadata"/>
411 <xslt:output method="xml" encoding="UTF-8" cdata-section-elements="html:script html:style html:textarea 书社:raw-output 书社:raw-text"/>
414 <output method="xml" encoding="UTF-8"/>