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