]> Lady’s Gitweb - Shushe/blobdiff - lib/serialize.xslt
Add support for manually serializing X·M·L
[Shushe] / lib / serialize.xslt
diff --git a/lib/serialize.xslt b/lib/serialize.xslt
new file mode 100644 (file)
index 0000000..5f792ea
--- /dev/null
@@ -0,0 +1,257 @@
+<?xml version="1.0"?>
+<!--
+SPDX-FileCopyrightText: 2024 Lady <https://www.ladys.computer/about/#lady>
+SPDX-License-Identifier: MPL-2.0
+-->
+<!--
+⁌ ⛩️📰 书社 ∷ lib/serialize.xslt
+
+© 2024 Lady [@ Lady’s Computer].
+
+This Source Code Form is subject to the terms of the Mozilla Public License, v 2.0.
+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/>.
+-->
+<transform
+       xmlns="http://www.w3.org/1999/XSL/Transform"
+       xmlns:exsl="http://exslt.org/common"
+       xmlns:exslstr="http://exslt.org/strings"
+       xmlns:html="http://www.w3.org/1999/xhtml"
+       xmlns:书社="urn:fdc:ladys.computer:20231231:Shu1She4"
+       extension-element-prefixes="exsl exslstr"
+       version="1.0"
+>
+       <template name="书社:sanitize-_amp_lt_gt">
+               <param name="source"/>
+               <choose>
+                       <when test="contains($source, '&amp;')">
+                               <call-template name="书社:sanitize-_lt_gt">
+                                       <with-param name="source" select="substring-before($source, '&amp;')"/>
+                               </call-template>
+                               <text>&amp;amp;</text>
+                               <call-template name="书社:sanitize-_amp_lt_gt">
+                                       <with-param name="source" select="substring-after($source, '&amp;')"/>
+                               </call-template>
+                       </when>
+                       <otherwise>
+                               <call-template name="书社:sanitize-_lt_gt">
+                                       <with-param name="source" select="$source"/>
+                               </call-template>
+                       </otherwise>
+               </choose>
+       </template>
+       <template name="书社:sanitize-_amp_quot_lt_gt">
+               <param name="source"/>
+               <choose>
+                       <when test="contains($source, '&amp;')">
+                               <call-template name="书社:sanitize-_quot_lt_gt">
+                                       <with-param name="source" select="substring-before($source, '&amp;')"/>
+                               </call-template>
+                               <text>&amp;amp;</text>
+                               <call-template name="书社:sanitize-_amp_quot_lt_gt">
+                                       <with-param name="source" select="substring-after($source, '&amp;')"/>
+                               </call-template>
+                       </when>
+                       <otherwise>
+                               <call-template name="书社:sanitize-_quot_lt_gt">
+                                       <with-param name="source" select="$source"/>
+                               </call-template>
+                       </otherwise>
+               </choose>
+       </template>
+       <template name="书社:sanitize-_gt">
+               <param name="source"/>
+               <choose>
+                       <when test="contains($source, '&gt;')">
+                               <value-of select="substring-before($source, '&gt;')"/>
+                               <text>&amp;gt;</text>
+                               <call-template name="书社:sanitize-_gt">
+                                       <with-param name="source" select="substring-after($source, '&gt;')"/>
+                               </call-template>
+                       </when>
+                       <otherwise>
+                               <value-of select="$source"/>
+                       </otherwise>
+               </choose>
+       </template>
+       <template name="书社:sanitize-_lt_gt">
+               <param name="source"/>
+               <choose>
+                       <when test="contains($source, '&lt;')">
+                               <call-template name="书社:sanitize-_gt">
+                                       <with-param name="source" select="substring-before($source, '&lt;')"/>
+                               </call-template>
+                               <text>&amp;lt;</text>
+                               <call-template name="书社:sanitize-_lt_gt">
+                                       <with-param name="source" select="substring-after($source, '&lt;')"/>
+                               </call-template>
+                       </when>
+                       <otherwise>
+                               <call-template name="书社:sanitize-_gt">
+                                       <with-param name="source" select="$source"/>
+                               </call-template>
+                       </otherwise>
+               </choose>
+       </template>
+       <template name="书社:sanitize-_quot_lt_gt">
+               <param name="source"/>
+               <choose>
+                       <when test="contains($source, '&quot;')">
+                               <call-template name="书社:sanitize-_lt_gt">
+                                       <with-param name="source" select="substring-before($source, '&quot;')"/>
+                               </call-template>
+                               <text>&amp;quot;</text>
+                               <call-template name="书社:sanitize-_quot_lt_gt">
+                                       <with-param name="source" select="substring-after($source, '&quot;')"/>
+                               </call-template>
+                       </when>
+                       <otherwise>
+                               <call-template name="书社:sanitize-_lt_gt">
+                                       <with-param name="source" select="$source"/>
+                               </call-template>
+                       </otherwise>
+               </choose>
+       </template>
+       <template name="书社:serialize-attribute-value">
+               <param name="source"/>
+               <call-template name="书社:sanitize-_amp_quot_lt_gt">
+                       <with-param name="source" select="string($source)"/>
+               </call-template>
+       </template>
+       <template name="书社:serialize-text-value">
+               <param name="source"/>
+               <call-template name="书社:sanitize-_amp_lt_gt">
+                       <with-param name="source" select="string($source)"/>
+               </call-template>
+       </template>
+       <template match="node()" mode="书社:serialize">
+               <param name="namespace" select="''"/>
+               <param name="declare-namespaces" select="''"/>
+               <param name="prefix-map">
+                       <html:dl>
+                               <html:div>
+                                       <html:dt>xml</html:dt>
+                                       <html:dd>http://www.w3.org/XML/1998/namespace</html:dd>
+                               </html:div>
+                       </html:dl>
+               </param>
+               <variable name="namespaces-to-declare" select="exslstr:tokenize($declare-namespaces)"/>
+               <choose>
+                       <when test="self::*">
+                               <variable name="local-namespace" select="string(namespace::*[local-name()=''])"/>
+                               <variable name="local-prefixes" select="namespace::*[local-name()!='' and local-name()!='xml']"/>
+                               <variable name="local-prefixes-map">
+                                       <html:dl>
+                                               <html:div>
+                                                       <html:dt>xml</html:dt>
+                                                       <html:dd>http://www.w3.org/XML/1998/namespace</html:dd>
+                                               </html:div>
+                                               <for-each select="exsl:node-set($prefix-map)//html:div">
+                                                       <if test="string(html:dt)!='xml' and not($local-prefixes[local-name()=string(current()/html:dt)])">
+                                                               <copy-of select="."/>
+                                                       </if>
+                                               </for-each>
+                                               <for-each select="$local-prefixes">
+                                                       <html:div>
+                                                               <html:dt>
+                                                                       <value-of select="local-name()"/>
+                                                               </html:dt>
+                                                               <html:dd>
+                                                                       <value-of select="string(.)"/>
+                                                               </html:dd>
+                                                       </html:div>
+                                               </for-each>
+                                       </html:dl>
+                               </variable>
+                               <variable name="qualified-name">
+                                       <choose>
+                                               <when test="$namespace=namespace-uri()">
+                                                       <if test="$namespace='http://www.w3.org/XML/1998/namespace'">
+                                                               <text>xml:</text>
+                                                       </if>
+                                                       <value-of select="local-name()"/>
+                                               </when>
+                                               <otherwise>
+                                                       <value-of select="name()"/>
+                                               </otherwise>
+                                       </choose>
+                               </variable>
+                               <text>&lt;</text>
+                               <value-of select="$qualified-name"/>
+                               <if test="$local-namespace!=$namespace or $namespaces-to-declare[string(.)=$local-namespace]">
+                                       <text> xmlns=&quot;</text>
+                                       <call-template name="书社:serialize-attribute-value">
+                                               <with-param name="source" select="$local-namespace"/>
+                                       </call-template>
+                                       <text>&quot;</text>
+                               </if>
+                               <for-each select="exsl:node-set($local-prefixes-map)//html:div">
+                                       <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)])">
+                                               <text> xmlns:</text>
+                                               <value-of select="html:dt"/>
+                                               <text>=&quot;</text>
+                                               <call-template name="书社:serialize-attribute-value">
+                                                       <with-param name="source" select="string(html:dd)"/>
+                                               </call-template>
+                                               <text>&quot;</text>
+                                       </if>
+                               </for-each>
+                               <for-each select="@*">
+                                       <text> </text>
+                                       <choose>
+                                               <when test="namespace-uri()='http://www.w3.org/XML/1998/namespace'">
+                                                       <text>xml:</text>
+                                                       <value-of select="local-name()"/>
+                                               </when>
+                                               <otherwise>
+                                                       <value-of select="name()"/>
+                                               </otherwise>
+                                       </choose>
+                                       <text>=&quot;</text>
+                                       <call-template name="书社:serialize-attribute-value">
+                                               <with-param name="source" select="string(.)"/>
+                                       </call-template>
+                                       <text>&quot;</text>
+                               </for-each>
+                               <choose>
+                                       <when test="node()">
+                                               <text>&gt;</text>
+                                               <apply-templates mode="书社:serialize">
+                                                       <with-param name="namespace" select="$local-namespace"/>
+                                                       <with-param name="prefix-map" select="$local-prefixes-map"/>
+                                               </apply-templates>
+                                               <text>&lt;/</text>
+                                               <value-of select="$qualified-name"/>
+                                               <text>&gt;</text>
+                                       </when>
+                                       <otherwise>
+                                               <text>/&gt;</text>
+                                       </otherwise>
+                               </choose>
+                       </when>
+                       <when test="node()">
+                               <apply-templates mode="书社:serialize">
+                                       <with-param name="namespace" select="$namespace"/>
+                                       <with-param name="prefix-map" select="$prefix-map"/>
+                               </apply-templates>
+                       </when>
+                       <when test="self::comment()">
+                               <text>&lt;!--</text>
+                               <value-of select="."/>
+                               <text>--&gt;</text>
+                       </when>
+                       <when test="self::text()">
+                               <call-template name="书社:serialize-text-value">
+                                       <with-param name="source" select="string(.)"/>
+                               </call-template>
+                       </when>
+                       <when test="self::processing-instruction()">
+                               <text>&lt;?</text>
+                               <value-of select="local-name()"/>
+                               <text> </text>
+                               <value-of select="."/>
+                               <text>?&gt;</text>
+                       </when>
+                       <otherwise/>
+               </choose>
+       </template>
+</transform>
This page took 0.029406 seconds and 4 git commands to generate.