]> Lady’s Gitweb - Shushe/blob - lib/serialize.xslt
Add support for manually serializing X·M·L
[Shushe] / lib / serialize.xslt
1 <?xml version="1.0"?>
2 <!--
3 SPDX-FileCopyrightText: 2024 Lady <https://www.ladys.computer/about/#lady>
4 SPDX-License-Identifier: MPL-2.0
5 -->
6 <!--
7 ⁌ ⛩️📰 书社 ∷ lib/serialize.xslt
8
9 © 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 <transform
15 xmlns="http://www.w3.org/1999/XSL/Transform"
16 xmlns:exsl="http://exslt.org/common"
17 xmlns:exslstr="http://exslt.org/strings"
18 xmlns:html="http://www.w3.org/1999/xhtml"
19 xmlns:书社="urn:fdc:ladys.computer:20231231:Shu1She4"
20 extension-element-prefixes="exsl exslstr"
21 version="1.0"
22 >
23 <template name="书社:sanitize-_amp_lt_gt">
24 <param name="source"/>
25 <choose>
26 <when test="contains($source, '&amp;')">
27 <call-template name="书社:sanitize-_lt_gt">
28 <with-param name="source" select="substring-before($source, '&amp;')"/>
29 </call-template>
30 <text>&amp;amp;</text>
31 <call-template name="书社:sanitize-_amp_lt_gt">
32 <with-param name="source" select="substring-after($source, '&amp;')"/>
33 </call-template>
34 </when>
35 <otherwise>
36 <call-template name="书社:sanitize-_lt_gt">
37 <with-param name="source" select="$source"/>
38 </call-template>
39 </otherwise>
40 </choose>
41 </template>
42 <template name="书社:sanitize-_amp_quot_lt_gt">
43 <param name="source"/>
44 <choose>
45 <when test="contains($source, '&amp;')">
46 <call-template name="书社:sanitize-_quot_lt_gt">
47 <with-param name="source" select="substring-before($source, '&amp;')"/>
48 </call-template>
49 <text>&amp;amp;</text>
50 <call-template name="书社:sanitize-_amp_quot_lt_gt">
51 <with-param name="source" select="substring-after($source, '&amp;')"/>
52 </call-template>
53 </when>
54 <otherwise>
55 <call-template name="书社:sanitize-_quot_lt_gt">
56 <with-param name="source" select="$source"/>
57 </call-template>
58 </otherwise>
59 </choose>
60 </template>
61 <template name="书社:sanitize-_gt">
62 <param name="source"/>
63 <choose>
64 <when test="contains($source, '&gt;')">
65 <value-of select="substring-before($source, '&gt;')"/>
66 <text>&amp;gt;</text>
67 <call-template name="书社:sanitize-_gt">
68 <with-param name="source" select="substring-after($source, '&gt;')"/>
69 </call-template>
70 </when>
71 <otherwise>
72 <value-of select="$source"/>
73 </otherwise>
74 </choose>
75 </template>
76 <template name="书社:sanitize-_lt_gt">
77 <param name="source"/>
78 <choose>
79 <when test="contains($source, '&lt;')">
80 <call-template name="书社:sanitize-_gt">
81 <with-param name="source" select="substring-before($source, '&lt;')"/>
82 </call-template>
83 <text>&amp;lt;</text>
84 <call-template name="书社:sanitize-_lt_gt">
85 <with-param name="source" select="substring-after($source, '&lt;')"/>
86 </call-template>
87 </when>
88 <otherwise>
89 <call-template name="书社:sanitize-_gt">
90 <with-param name="source" select="$source"/>
91 </call-template>
92 </otherwise>
93 </choose>
94 </template>
95 <template name="书社:sanitize-_quot_lt_gt">
96 <param name="source"/>
97 <choose>
98 <when test="contains($source, '&quot;')">
99 <call-template name="书社:sanitize-_lt_gt">
100 <with-param name="source" select="substring-before($source, '&quot;')"/>
101 </call-template>
102 <text>&amp;quot;</text>
103 <call-template name="书社:sanitize-_quot_lt_gt">
104 <with-param name="source" select="substring-after($source, '&quot;')"/>
105 </call-template>
106 </when>
107 <otherwise>
108 <call-template name="书社:sanitize-_lt_gt">
109 <with-param name="source" select="$source"/>
110 </call-template>
111 </otherwise>
112 </choose>
113 </template>
114 <template name="书社:serialize-attribute-value">
115 <param name="source"/>
116 <call-template name="书社:sanitize-_amp_quot_lt_gt">
117 <with-param name="source" select="string($source)"/>
118 </call-template>
119 </template>
120 <template name="书社:serialize-text-value">
121 <param name="source"/>
122 <call-template name="书社:sanitize-_amp_lt_gt">
123 <with-param name="source" select="string($source)"/>
124 </call-template>
125 </template>
126 <template match="node()" mode="书社:serialize">
127 <param name="namespace" select="''"/>
128 <param name="declare-namespaces" select="''"/>
129 <param name="prefix-map">
130 <html:dl>
131 <html:div>
132 <html:dt>xml</html:dt>
133 <html:dd>http://www.w3.org/XML/1998/namespace</html:dd>
134 </html:div>
135 </html:dl>
136 </param>
137 <variable name="namespaces-to-declare" select="exslstr:tokenize($declare-namespaces)"/>
138 <choose>
139 <when test="self::*">
140 <variable name="local-namespace" select="string(namespace::*[local-name()=''])"/>
141 <variable name="local-prefixes" select="namespace::*[local-name()!='' and local-name()!='xml']"/>
142 <variable name="local-prefixes-map">
143 <html:dl>
144 <html:div>
145 <html:dt>xml</html:dt>
146 <html:dd>http://www.w3.org/XML/1998/namespace</html:dd>
147 </html:div>
148 <for-each select="exsl:node-set($prefix-map)//html:div">
149 <if test="string(html:dt)!='xml' and not($local-prefixes[local-name()=string(current()/html:dt)])">
150 <copy-of select="."/>
151 </if>
152 </for-each>
153 <for-each select="$local-prefixes">
154 <html:div>
155 <html:dt>
156 <value-of select="local-name()"/>
157 </html:dt>
158 <html:dd>
159 <value-of select="string(.)"/>
160 </html:dd>
161 </html:div>
162 </for-each>
163 </html:dl>
164 </variable>
165 <variable name="qualified-name">
166 <choose>
167 <when test="$namespace=namespace-uri()">
168 <if test="$namespace='http://www.w3.org/XML/1998/namespace'">
169 <text>xml:</text>
170 </if>
171 <value-of select="local-name()"/>
172 </when>
173 <otherwise>
174 <value-of select="name()"/>
175 </otherwise>
176 </choose>
177 </variable>
178 <text>&lt;</text>
179 <value-of select="$qualified-name"/>
180 <if test="$local-namespace!=$namespace or $namespaces-to-declare[string(.)=$local-namespace]">
181 <text> xmlns=&quot;</text>
182 <call-template name="书社:serialize-attribute-value">
183 <with-param name="source" select="$local-namespace"/>
184 </call-template>
185 <text>&quot;</text>
186 </if>
187 <for-each select="exsl:node-set($local-prefixes-map)//html:div">
188 <if test="$namespaces-to-declare[string(.)=string(current()/html:dd)] or string(html:dt)!='xml' and not(exsl:node-set($prefix-map)//html:div[string(html:dt)=string(current()/html:dt) and string(html:dd)=string(current()/html:dd)])">
189 <text> xmlns:</text>
190 <value-of select="html:dt"/>
191 <text>=&quot;</text>
192 <call-template name="书社:serialize-attribute-value">
193 <with-param name="source" select="string(html:dd)"/>
194 </call-template>
195 <text>&quot;</text>
196 </if>
197 </for-each>
198 <for-each select="@*">
199 <text> </text>
200 <choose>
201 <when test="namespace-uri()='http://www.w3.org/XML/1998/namespace'">
202 <text>xml:</text>
203 <value-of select="local-name()"/>
204 </when>
205 <otherwise>
206 <value-of select="name()"/>
207 </otherwise>
208 </choose>
209 <text>=&quot;</text>
210 <call-template name="书社:serialize-attribute-value">
211 <with-param name="source" select="string(.)"/>
212 </call-template>
213 <text>&quot;</text>
214 </for-each>
215 <choose>
216 <when test="node()">
217 <text>&gt;</text>
218 <apply-templates mode="书社:serialize">
219 <with-param name="namespace" select="$local-namespace"/>
220 <with-param name="prefix-map" select="$local-prefixes-map"/>
221 </apply-templates>
222 <text>&lt;/</text>
223 <value-of select="$qualified-name"/>
224 <text>&gt;</text>
225 </when>
226 <otherwise>
227 <text>/&gt;</text>
228 </otherwise>
229 </choose>
230 </when>
231 <when test="node()">
232 <apply-templates mode="书社:serialize">
233 <with-param name="namespace" select="$namespace"/>
234 <with-param name="prefix-map" select="$prefix-map"/>
235 </apply-templates>
236 </when>
237 <when test="self::comment()">
238 <text>&lt;!--</text>
239 <value-of select="."/>
240 <text>--&gt;</text>
241 </when>
242 <when test="self::text()">
243 <call-template name="书社:serialize-text-value">
244 <with-param name="source" select="string(.)"/>
245 </call-template>
246 </when>
247 <when test="self::processing-instruction()">
248 <text>&lt;?</text>
249 <value-of select="local-name()"/>
250 <text> </text>
251 <value-of select="."/>
252 <text>?&gt;</text>
253 </when>
254 <otherwise/>
255 </choose>
256 </template>
257 </transform>
This page took 0.19536 seconds and 5 git commands to generate.