3 SPDX-FileCopyrightText: 2024 Lady <https://www.ladys.computer/about/#lady>
4 SPDX-License-Identifier: MPL-2.0
7 ⁌ ⛩📰 书社 ∷ lib/serialize.xslt
9 © 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 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"
23 <template name="书社:sanitize-_amp_lt_gt">
24 <param name="source"/>
26 <when test="contains($source, '&')">
27 <call-template name="书社:sanitize-_lt_gt">
28 <with-param name="source" select="substring-before($source, '&')"/>
30 <text>&amp;</text>
31 <call-template name="书社:sanitize-_amp_lt_gt">
32 <with-param name="source" select="substring-after($source, '&')"/>
36 <call-template name="书社:sanitize-_lt_gt">
37 <with-param name="source" select="$source"/>
42 <template name="书社:sanitize-_amp_quot_lt_gt">
43 <param name="source"/>
45 <when test="contains($source, '&')">
46 <call-template name="书社:sanitize-_quot_lt_gt">
47 <with-param name="source" select="substring-before($source, '&')"/>
49 <text>&amp;</text>
50 <call-template name="书社:sanitize-_amp_quot_lt_gt">
51 <with-param name="source" select="substring-after($source, '&')"/>
55 <call-template name="书社:sanitize-_quot_lt_gt">
56 <with-param name="source" select="$source"/>
61 <template name="书社:sanitize-_gt">
62 <param name="source"/>
64 <when test="contains($source, '>')">
65 <value-of select="substring-before($source, '>')"/>
67 <call-template name="书社:sanitize-_gt">
68 <with-param name="source" select="substring-after($source, '>')"/>
72 <value-of select="$source"/>
76 <template name="书社:sanitize-_lt_gt">
77 <param name="source"/>
79 <when test="contains($source, '<')">
80 <call-template name="书社:sanitize-_gt">
81 <with-param name="source" select="substring-before($source, '<')"/>
84 <call-template name="书社:sanitize-_lt_gt">
85 <with-param name="source" select="substring-after($source, '<')"/>
89 <call-template name="书社:sanitize-_gt">
90 <with-param name="source" select="$source"/>
95 <template name="书社:sanitize-_quot_lt_gt">
96 <param name="source"/>
98 <when test="contains($source, '"')">
99 <call-template name="书社:sanitize-_lt_gt">
100 <with-param name="source" select="substring-before($source, '"')"/>
102 <text>&quot;</text>
103 <call-template name="书社:sanitize-_quot_lt_gt">
104 <with-param name="source" select="substring-after($source, '"')"/>
108 <call-template name="书社:sanitize-_lt_gt">
109 <with-param name="source" select="$source"/>
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)"/>
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)"/>
126 <template match="node()" mode="书社:serialize" priority="0">
127 <param name="namespace" select="''"/>
128 <param name="declare-namespaces" select="''"/>
129 <param name="prefix-map">
132 <html:dt>xml</html:dt>
133 <html:dd>http://www.w3.org/XML/1998/namespace</html:dd>
137 <variable name="namespaces-to-declare" select="exslstr:tokenize($declare-namespaces)"/>
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">
145 <html:dt>xml</html:dt>
146 <html:dd>http://www.w3.org/XML/1998/namespace</html:dd>
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="."/>
153 <for-each select="$local-prefixes">
156 <value-of select="local-name()"/>
159 <value-of select="string(.)"/>
165 <variable name="qualified-name">
167 <when test="$namespace=namespace-uri()">
168 <if test="$namespace='http://www.w3.org/XML/1998/namespace'">
171 <value-of select="local-name()"/>
174 <value-of select="name()"/>
179 <value-of select="$qualified-name"/>
180 <if test="$local-namespace!=$namespace or $namespaces-to-declare[string(.)=$local-namespace]">
181 <text> xmlns="</text>
182 <call-template name="书社:serialize-attribute-value">
183 <with-param name="source" select="$local-namespace"/>
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)])">
190 <value-of select="html:dt"/>
192 <call-template name="书社:serialize-attribute-value">
193 <with-param name="source" select="string(html:dd)"/>
198 <for-each select="@*">
201 <when test="namespace-uri()='http://www.w3.org/XML/1998/namespace'">
203 <value-of select="local-name()"/>
206 <value-of select="name()"/>
210 <call-template name="书社:serialize-attribute-value">
211 <with-param name="source" select="string(.)"/>
218 <apply-templates mode="书社:serialize">
219 <with-param name="namespace" select="$local-namespace"/>
220 <with-param name="prefix-map" select="$local-prefixes-map"/>
223 <value-of select="$qualified-name"/>
232 <apply-templates mode="书社:serialize">
233 <with-param name="namespace" select="$namespace"/>
234 <with-param name="prefix-map" select="$prefix-map"/>
237 <when test="self::comment()">
239 <value-of select="."/>
242 <when test="self::text()">
243 <call-template name="书社:serialize-text-value">
244 <with-param name="source" select="string(.)"/>
247 <when test="self::processing-instruction()">
249 <value-of select="local-name()"/>
251 <value-of select="."/>