]> Lady’s Gitweb - Shushe/blob - lib/catalog2parser.xslt
Support parsed metadata
[Shushe] / lib / catalog2parser.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/catalog2parser.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 rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
16 ]>
17 <transform
18 xmlns="http://www.w3.org/1999/XSL/Transform"
19 xmlns:catalog="urn:oasis:names:tc:entity:xmlns:xml:catalog"
20 xmlns:exsl="http://exslt.org/common"
21 xmlns:exslstr="http://exslt.org/strings"
22 xmlns:html="http://www.w3.org/1999/xhtml"
23 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
24 xmlns:xslt="http://www.w3.org/1999/XSL/TransformAlias"
25 xmlns:书社="urn:fdc:ladys.computer:20231231:Shu1She4"
26 exclude-result-prefixes="catalog"
27 extension-element-prefixes="exslstr"
28 version="1.0"
29 >
30 <namespace-alias stylesheet-prefix="xslt" result-prefix="#default"/>
31 <template match="/">
32 <xslt:transform extension-element-prefixes="exsl" version="1.0">
33 <xslt:param name="BUILDTIME" select="'1972-12-31T00:00:00Z'"/>
34 <xslt:param name="IDENTIFIER" select="false()"/>
35 <xslt:param name="SRCREV" select="false()"/>
36 <xslt:param name="THISREV" select="false()"/>
37 <xslt:variable name="书社:parsers">
38 <html:dl>
39 <for-each select="//catalog:uri">
40 <variable name="parser" select="document(@uri, .)"/>
41 <html:div>
42 <html:dt>
43 <value-of select="@name"/>
44 </html:dt>
45 <choose>
46 <when test="$parser/*/@书社:supported-media-types">
47 <for-each select="exslstr:tokenize($parser/*/@书社:supported-media-types)">
48 <if test="not(translate(., '0123456789abcdefghijklmnopqrstuvwxyz!#$&#x26;-^_.+/', ''))">
49 <html:dd>
50 <value-of select="."/>
51 </html:dd>
52 </if>
53 </for-each>
54 </when>
55 <otherwise>
56 <for-each select="$parser//xsl:template[not(@name) and not(@mode)]">
57 <variable name="match" select="@match"/>
58 <for-each select="namespace::*[local-name() and string()='http://www.w3.org/1999/xhtml']">
59 <variable name="matchstart">
60 <value-of select="local-name()"/>
61 <text>:</text>
62 <text>script[@type=</text>
63 </variable>
64 <if test="starts-with($match, $matchstart) and substring($match, string-length($match))=']' and contains($match, '/')">
65 <variable name="inner" select="substring-before(substring-after($match, $matchstart), ']')"/>
66 <if test="starts-with($inner, '&#x22;') and substring($inner, string-length($inner))='&#x22;' or starts-with($inner, &#x22;'&#x22;) and substring($inner, string-length($inner))=&#x22;'&#x22;">
67 <variable name="type" select="substring($inner, 2, string-length($inner)-2)"/>
68 <if test="not(translate($type, '0123456789abcdefghijklmnopqrstuvwxyz!#$&#x26;-^_.+/', ''))">
69 <html:dd>
70 <value-of select="$type"/>
71 </html:dd>
72 </if>
73 </if>
74 </if>
75 </for-each>
76 </for-each>
77 </otherwise>
78 </choose>
79 </html:div>
80 </for-each>
81 </html:dl>
82 </xslt:variable>
83 <for-each select="//catalog:uri">
84 <xslt:include href="{@uri}">
85 <if test="contains(@name, ':')">
86 <attribute name="书社:id">
87 <value-of select="@name"/>
88 </attribute>
89 </if>
90 </xslt:include>
91 </for-each>
92 <xslt:template name="书社:apply-parsed-by">
93 <xslt:param name="id"/>
94 <xslt:param name="media-type"/>
95 <xslt:param name="result" select="/.."/>
96 <xslt:for-each select="$result/node()">
97 <xslt:choose>
98 <xslt:when test="self::*">
99 <xslt:copy>
100 <xslt:if test="string($id)!=''">
101 <xslt:attribute name="书社:parsed-by">
102 <xslt:value-of select="$id"/>
103 <xslt:if test="@书社:parsed-by">
104 <xslt:text>
105 <text> </text>
106 </xslt:text>
107 <xslt:value-of select="@书社:parsed-by"/>
108 </xslt:if>
109 </xslt:attribute>
110 </xslt:if>
111 <xslt:if test="string($media-type)!=''">
112 <xslt:attribute name="书社:media-type">
113 <xslt:value-of select="$media-type"/>
114 </xslt:attribute>
115 </xslt:if>
116 <xslt:copy-of select="@*[namespace-uri()!='urn:fdc:ladys.computer:20231231:Shu1She4' or local-name()!='parsed-by' and local-name()!='media-type']|node()"/>
117 </xslt:copy>
118 </xslt:when>
119 <xslt:otherwise>
120 <xslt:copy-of select="."/>
121 </xslt:otherwise>
122 </xslt:choose>
123 </xslt:for-each>
124 </xslt:template>
125 <xslt:template match="/">
126 <xslt:variable name="about">
127 <xslt:apply-templates select="node()" mode="书社:about"/>
128 </xslt:variable>
129 <xslt:variable name="result">
130 <xslt:apply-templates select="node()" mode="书社:parse"/>
131 </xslt:variable>
132 <xslt:element name="书社:parsed">
133 <xslt:element name="书社:result">
134 <xslt:copy-of select="$result"/>
135 </xslt:element>
136 <xslt:element name="书社:metadata">
137 <xslt:element name="rdf:RDF" namespace="&rdf;">
138 <xslt:element name="rdf:Description" namespace="&rdf;">
139 <xslt:copy-of select="$about"/>
140 </xslt:element>
141 </xslt:element>
142 </xslt:element>
143 </xslt:element>
144 </xslt:template>
145 <xslt:template match="@*|node()" priority="-1">
146 <xslt:copy>
147 <xslt:apply-templates select="@*|node()" mode="书社:parse"/>
148 </xslt:copy>
149 </xslt:template>
150 <xslt:template match="@*|node()" mode="书社:about" priority="-1">
151 <xslt:apply-templates select="@*|node()" mode="书社:about"/>
152 </xslt:template>
153 <xslt:template match="html:script[@type]" mode="书社:parse" priority="1">
154 <xslt:variable name="parserdiv" select="exsl:node-set($书社:parsers)//html:div[html:dd=current()/@type]"/>
155 <xslt:choose>
156 <xslt:when test="$parserdiv">
157 <xslt:variable name="result">
158 <xslt:apply-templates select="."/>
159 </xslt:variable>
160 <xslt:variable name="reparsed-result">
161 <xslt:apply-templates select="exsl:node-set($result)/node()" mode="书社:parse"/>
162 </xslt:variable>
163 <xslt:call-template name="书社:apply-parsed-by">
164 <xslt:with-param name="id" select="string($parserdiv/html:dt)"/>
165 <xslt:with-param name="media-type" select="string(@type)"/>
166 <xslt:with-param name="result" select="exsl:node-set($reparsed-result)"/>
167 </xslt:call-template>
168 </xslt:when>
169 <xslt:otherwise>
170 <xslt:apply-templates select="."/>
171 </xslt:otherwise>
172 </xslt:choose>
173 </xslt:template>
174 <xslt:template match="@*|node()" mode="书社:parse">
175 <xslt:apply-templates select="."/>
176 </xslt:template>
177 </xslt:transform>
178 </template>
179 <output method="xml" encoding="UTF-8"/>
180 </transform>
This page took 0.0654 seconds and 5 git commands to generate.