]> Lady’s Gitweb - Shushe/blob - lib/catalog2transform.xslt
Refactor transforms & add 书社:application stage
[Shushe] / lib / catalog2transform.xslt
1 <?xml version="1.0"?>
2 <!--
3 ⁌ ⛩️📰 书社 ∷ lib/catalog2transform.xslt
4
5 © 2023–2024 Lady [@ Lady’s Computer]
6
7 This Source Code Form is subject to the terms of the Mozilla Public License, v 2.0.
8 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/>.
9 -->
10 <!DOCTYPE transform [
11 <!ENTITY 书社 "urn:fdc:ladys.computer:20231231:Shu1She4">
12 <!ENTITY xml "http://www.w3.org/XML/1998/namespace">
13 ]>
14 <transform
15 xmlns="http://www.w3.org/1999/XSL/Transform"
16 xmlns:html="http://www.w3.org/1999/xhtml"
17 xmlns:catalog="urn:oasis:names:tc:entity:xmlns:xml:catalog"
18 xmlns:exsl="http://exslt.org/common"
19 xmlns:exslstr="http://exslt.org/strings"
20 xmlns:svg="http://www.w3.org/2000/svg"
21 xmlns:xlink="http://www.w3.org/1999/xlink"
22 xmlns:xslt="http://www.w3.org/1999/XSL/TransformAlias"
23 xmlns:书社="urn:fdc:ladys.computer:20231231:Shu1She4"
24 version="1.0"
25 >
26 <namespace-alias stylesheet-prefix="xslt" result-prefix="#default"/>
27 <template match="/">
28 <xslt:transform exclude-result-prefixes="catalog exsl exslstr" version="1.0">
29 <xslt:param name="BUILDTIME" select="'1972-12-31T00:00:00Z'"/>
30 <xslt:param name="CKSUM" select="false"/>
31 <xslt:param name="CATALOG" select="'catalog'"/>
32 <xslt:param name="IDENTIFIER" select="false"/>
33 <xslt:param name="PATH" select="'/unknown'"/>
34 <xslt:param name="SRCREV" select="false"/>
35 <xslt:param name="SRCTIME" select="'1972-12-31T00:00:00Z'"/>
36 <xslt:param name="THISREV" select="false"/>
37 <xslt:variable name="书社:source" select="/"/>
38 <xslt:variable name="书社:expansion">
39 <xslt:apply-templates select="$书社:source/node()" mode="书社:expand"/>
40 </xslt:variable>
41 <xslt:variable name="书社:result">
42 <xslt:apply-templates select="exsl:node-set($书社:expansion)/node()"/>
43 </xslt:variable>
44 <for-each select="//catalog:uri">
45 <xslt:include href="{@uri}">
46 <if test="contains(@name, ':')">
47 <attribute name="书社:id">
48 <value-of select="@name"/>
49 </attribute>
50 </if>
51 </xslt:include>
52 </for-each>
53 <xslt:template name="书社:apply-attributes">
54 <xslt:param name="and-version" select="false"/>
55 <xslt:param name="context-nodes" select="/.."/>
56 <xslt:param name="destination-nodes" select="/.."/>
57 <xslt:variable name="additional-attributes" select="$context-nodes/@*"/>
58 <xslt:for-each select="$destination-nodes">
59 <xslt:variable name="existing-attributes" select="@*"/>
60 <xslt:choose>
61 <xslt:when test="self::*">
62 <xslt:variable name="context" select="."/>
63 <xslt:variable name="attribute-names">
64 <xslt:for-each select="$existing-attributes|$additional-attributes">
65 <书社:attribute>
66 <attribute name="local-name">
67 <text>{local-name()}</text>
68 </attribute>
69 <attribute name="name">
70 <text>{name()}</text>
71 </attribute>
72 <attribute name="namespace-uri">
73 <text>{namespace-uri()}</text>
74 </attribute>
75 </书社:attribute>
76 </xslt:for-each>
77 </xslt:variable>
78 <xslt:variable name="lang">
79 <xslt:choose>
80 <xslt:when test="(self::html:*|self::svg:*)/@lang">
81 <xslt:value-of select="@lang"/>
82 </xslt:when>
83 <xslt:when test="@xml:lang">
84 <xslt:value-of select="@xml:lang"/>
85 </xslt:when>
86 <xslt:when test="$additional-attributes[(parent::html:* or parent::svg:*) and namespace-uri()='' and local-name()='lang']">
87 <xslt:value-of select="$additional-attributes[(parent::html:* or parent::svg:*) and namespace-uri()='' and local-name()='lang']"/>
88 </xslt:when>
89 <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']">
90 <xslt:value-of select="$additional-attributes[(parent::书社:apply-attributes or parent::书社:apply-attributes-to-root) and namespace-uri()='' and local-name()='lang']"/>
91 </xslt:when>
92 <xslt:when test="$additional-attributes[namespace-uri()='&xml;' and local-name()='lang']">
93 <xslt:value-of select="$additional-attributes[namespace-uri()='&xml;' and local-name()='lang']"/>
94 </xslt:when>
95 </xslt:choose>
96 </xslt:variable>
97 <xslt:copy>
98 <xslt:if test="$and-version and $THISREV">
99 <xslt:attribute name="书社:version">
100 <xslt:value-of select="$THISREV"/>
101 </xslt:attribute>
102 </xslt:if>
103 <xslt:if test="string($lang)!=''">
104 <xslt:if test="self::html:* or self::svg:*">
105 <xslt:attribute name="lang">
106 <xslt:value-of select="$lang"/>
107 </xslt:attribute>
108 </xslt:if>
109 <xslt:attribute name="xml:lang">
110 <xslt:value-of select="$lang"/>
111 </xslt:attribute>
112 </xslt:if>
113 <xslt:for-each select="exsl:node-set($attribute-names)/*">
114 <xslt:choose>
115 <xslt:when test="@namespace-uri='&xml;'"/>
116 <xslt:when test="$context[self::html:* or self::svg:*] and @namespace-uri='' and @local-name='lang'"/>
117 <xslt:when test="@namespace-uri='&书社;' and (@local-name='destination' or @local-name='disable-output-wrapping' or @local-name='version')"/>
118 <xslt:when test="preceding-sibling::*[@local-name=current()/@local-name and @namespace-uri=current()/@namespace-uri]"/>
119 <xslt:otherwise>
120 <xslt:attribute>
121 <attribute name="name">
122 <text>{@name}</text>
123 </attribute>
124 <attribute name="namespace">
125 <text>{@namespace-uri}</text>
126 </attribute>
127 <xslt:for-each select="$existing-attributes[local-name()=current()/@local-name and namespace-uri()=current()/@namespace-uri]">
128 <xslt:value-of select="."/>
129 <xslt:if test="position()!=last()">
130 <xslt:text>
131 <text> </text>
132 </xslt:text>
133 </xslt:if>
134 </xslt:for-each>
135 <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]">
136 <xslt:text>
137 <text> </text>
138 </xslt:text>
139 </xslt:if>
140 <xslt:for-each select="$additional-attributes[local-name()=current()/@local-name and namespace-uri()=current()/@namespace-uri]">
141 <xslt:value-of select="."/>
142 <xslt:if test="position()!=last()">
143 <xslt:text>
144 <text> </text>
145 </xslt:text>
146 </xslt:if>
147 </xslt:for-each>
148 </xslt:attribute>
149 </xslt:otherwise>
150 </xslt:choose>
151 </xslt:for-each>
152 <xslt:copy-of select="node()"/>
153 </xslt:copy>
154 </xslt:when>
155 <xslt:otherwise>
156 <xslt:copy-of select="."/>
157 </xslt:otherwise>
158 </xslt:choose>
159 </xslt:for-each>
160 </xslt:template>
161 <xslt:template name="书社:wrap">
162 <xslt:param name="nodes" select="/.."/>
163 <xslt:variable name="modalinput">
164 <xslt:copy-of select="$nodes"/>
165 <xslt:copy-of select="document('')/xslt:transform/xslt:include"/>
166 </xslt:variable>
167 <xslt:variable name="metadata">
168 <xslt:copy-of select="$nodes[self::html:html]/html:head/node()|$nodes[self::html:head]/node()"/>
169 <xslt:apply-templates select="exsl:node-set($modalinput)/node()" mode="书社:metadata"/>
170 </xslt:variable>
171 <html:html>
172 <xslt:copy-of select="$nodes[self::html:html]/@*"/>
173 <xslt:if test="not($nodes[self::html:html]/@lang) and $nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang">
174 <xslt:attribute name="lang">
175 <xslt:value-of select="$nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang"/>
176 </xslt:attribute>
177 </xslt:if>
178 <xslt:if test="not($nodes[self::html:html]/@xml:lang) and $nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang">
179 <xslt:attribute name="xml:lang">
180 <xslt:value-of select="$nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang"/>
181 </xslt:attribute>
182 </xslt:if>
183 <html:head>
184 <xslt:copy-of select="$nodes[self::html:html]/html:head/@*|$nodes[self::html:head]/@*"/>
185 <html:title>
186 <xslt:for-each select="exsl:node-set($metadata)/html:title">
187 <xslt:value-of select="."/>
188 <xslt:if test="position()!=last()">
189 <xslt:text>
190 <text> </text>
191 </xslt:text>
192 </xslt:if>
193 </xslt:for-each>
194 </html:title>
195 <xslt:copy-of select="exsl:node-set($metadata)/node()[not(self::html:title)]"/>
196 <html:meta name="generator">
197 <xslt:attribute name="content">
198 <xslt:for-each select="exsl:node-set($metadata)/html:meta[@name='generator']">
199 <xslt:value-of select="@content"/>
200 <xslt:text>
201 <text>, </text>
202 </xslt:text>
203 </xslt:for-each>
204 <xslt:text>
205 <text>⛩️📰 书社</text>
206 </xslt:text>
207 <xslt:if test="$THISREV">
208 <xslt:text>
209 <text> (</text>
210 </xslt:text>
211 <xslt:value-of select="$THISREV"/>
212 <xslt:text>
213 <text>)</text>
214 </xslt:text>
215 </xslt:if>
216 </xslt:attribute>
217 </html:meta>
218 </html:head>
219 <html:body>
220 <xslt:copy-of select="$nodes[self::html:html]/html:body/@*|$nodes[self::html:body]/@*"/>
221 <xslt:apply-templates select="exsl:node-set($modalinput)/node()" mode="书社:header"/>
222 <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()"/>
223 <xslt:apply-templates select="exsl:node-set($modalinput)/node()" mode="书社:footer"/>
224 </html:body>
225 </html:html>
226 </xslt:template>
227 <xslt:template match="/" priority="1">
228 <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)])]"/>
229 <xslt:variable name="root-with-attributes">
230 <xslt:choose>
231 <xslt:when test="$result-nodes/@书社:disable-output-wrapping|exsl:node-set($书社:result)//书社:apply-attributes-to-root/@书社:disable-output-wrapping">
232 <xslt:call-template name="书社:apply-attributes">
233 <xslt:with-param name="and-version" select="true"/>
234 <xslt:with-param name="context-nodes" select="exsl:node-set($书社:result)//书社:apply-attributes-to-root"/>
235 <xslt:with-param name="destination-nodes" select="$result-nodes"/>
236 </xslt:call-template>
237 </xslt:when>
238 <xslt:otherwise>
239 <xslt:variable name="wrapped-result">
240 <xslt:call-template name="书社:wrap">
241 <xslt:with-param name="nodes" select="$result-nodes"/>
242 </xslt:call-template>
243 </xslt:variable>
244 <xslt:call-template name="书社:apply-attributes">
245 <xslt:with-param name="and-version" select="true"/>
246 <xslt:with-param name="context-nodes" select="exsl:node-set($书社:result)//书社:apply-attributes-to-root"/>
247 <xslt:with-param name="destination-nodes" select="exsl:node-set($wrapped-result)/node()"/>
248 </xslt:call-template>
249 </xslt:otherwise>
250 </xslt:choose>
251 </xslt:variable>
252 <xslt:apply-templates select="exsl:node-set($root-with-attributes)/node()" mode="书社:application"/>
253 </xslt:template>
254 <xslt:template match="@*|node()" priority="-1">
255 <xslt:copy>
256 <xslt:apply-templates select="@*|node()"/>
257 </xslt:copy>
258 </xslt:template>
259 <xslt:template match="@*|node()" mode="书社:application">
260 <xslt:copy>
261 <xslt:apply-templates select="@*|node()" mode="书社:application"/>
262 </xslt:copy>
263 </xslt:template>
264 <xslt:template match="@书社:destination|@书社:disable-output-wrapping" mode="书社:application" priority="1"/>
265 <xslt:template match="书社:apply-attributes" mode="书社:application" priority="1">
266 <xslt:variable name="children">
267 <xslt:apply-templates select="node()" mode="书社:application"/>
268 </xslt:variable>
269 <xslt:call-template name="书社:apply-attributes">
270 <xslt:with-param name="context-nodes" select="."/>
271 <xslt:with-param name="destination-nodes" select="exsl:node-set($children)/node()"/>
272 </xslt:call-template>
273 </xslt:template>
274 <xslt:template match="书社:apply-attributes-to-root" mode="书社:application" priority="1">
275 <xslt:apply-templates select="node()" mode="书社:application"/>
276 </xslt:template>
277 <xslt:template match="/node()" mode="书社:expand" priority="0">
278 <xslt:copy>
279 <xslt:attribute name="书社:identifier">
280 <xslt:value-of select="$IDENTIFIER"/>
281 </xslt:attribute>
282 <xslt:choose>
283 <xslt:when test="self::html:*">
284 <xslt:attribute name="itemscope">itemscope</xslt:attribute>
285 <xslt:attribute name="itemtype">
286 <xslt:text>
287 <text>&书社;:document</text>
288 </xslt:text>
289 <xslt:for-each select="exslstr:tokenize(@itemtype)/token">
290 <xslt:text>
291 <text> </text>
292 </xslt:text>
293 <xslt:value-of select="."/>
294 </xslt:for-each>
295 </xslt:attribute>
296 <xslt:apply-templates select="@*[not(namespace-uri()='' and (local-name()='itemscope' or local-name()='itemtype'))]|node()" mode="书社:expand"/>
297 </xslt:when>
298 <xslt:otherwise>
299 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
300 </xslt:otherwise>
301 </xslt:choose>
302 </xslt:copy>
303 </xslt:template>
304 <xslt:template match="书社:link[@xlink:show='embed']" mode="书社:expand" priority="1">
305 <xslt:variable name="identifier" select="string(@xlink:href)"/>
306 <xslt:variable name="uri" select="substring-before(document($CATALOG)//catalog:uri[@name=$identifier]/@uri[1], '#')"/>
307 <xslt:choose>
308 <xslt:when test="$uri">
309 <xslt:variable name="expanded">
310 <xslt:apply-templates select="document($uri)" mode="书社:expand"/>
311 </xslt:variable>
312 <xslt:for-each select="exsl:node-set($expanded)/node()">
313 <xslt:copy>
314 <xslt:attribute name="书社:identifier">
315 <xslt:value-of select="$identifier"/>
316 </xslt:attribute>
317 <xslt:choose>
318 <xslt:when test="self::html:*">
319 <xslt:attribute name="itemscope">itemscope</xslt:attribute>
320 <xslt:attribute name="itemtype">
321 <xslt:text>
322 <text>&书社;:embed</text>
323 </xslt:text>
324 <xslt:for-each select="exslstr:tokenize(@itemtype)/token[string()!='&书社;:document']">
325 <xslt:text>
326 <text> </text>
327 </xslt:text>
328 <xslt:value-of select="."/>
329 </xslt:for-each>
330 </xslt:attribute>
331 <xslt:copy-of select="@*[not(namespace-uri()='' and (local-name()='itemscope' or local-name()='itemtype')) and not(namespace-uri()='&书社;' and local-name()='identifier')]|node()"/>
332 </xslt:when>
333 <xslt:otherwise>
334 <xslt:apply-templates select="@*[not(namespace-uri()='&书社;' and local-name()='identifier')]|node()" mode="书社:expand"/>
335 </xslt:otherwise>
336 </xslt:choose>
337 </xslt:copy>
338 </xslt:for-each>
339 </xslt:when>
340 <xslt:otherwise>
341 <xslt:copy>
342 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
343 </xslt:copy>
344 </xslt:otherwise>
345 </xslt:choose>
346 </xslt:template>
347 <xslt:template match="@*|node()[not(self::书社:link) or not(@xlink:show='embed')]" mode="书社:expand" priority="-1">
348 <xslt:copy>
349 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
350 </xslt:copy>
351 </xslt:template>
352 <xslt:template match="text()" mode="书社:header"/>
353 <xslt:template match="text()" mode="书社:footer"/>
354 <xslt:template match="text()" mode="书社:metadata"/>
355 <xslt:output method="xml" encoding="UTF-8" cdata-section-elements="html:script html:style html:textarea"/>
356 </xslt:transform>
357 </template>
358 <output method="xml" encoding="UTF-8"/>
359 </transform>
This page took 0.08139 seconds and 5 git commands to generate.