]> git.stg.codes - stg.git/blob - doc/xslt/common/stripns.xsl
Set output encoding to utf-8.
[stg.git] / doc / xslt / common / stripns.xsl
1 <?xml version='1.0'?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                 xmlns:ng="http://docbook.org/docbook-ng"
4                 xmlns:db="http://docbook.org/ns/docbook"
5                 xmlns:saxon="http://icl.com/saxon"
6                 xmlns:NodeInfo="http://org.apache.xalan.lib.NodeInfo"
7                 xmlns:exsl="http://exslt.org/common"
8                 xmlns:xlink="http://www.w3.org/1999/xlink"
9                 exclude-result-prefixes="db ng exsl saxon NodeInfo xlink"
10                 version='1.0'>
11
12 <!-- ********************************************************************
13      $Id: stripns.xsl 8728 2010-07-15 14:37:33Z mzjn $
14      ********************************************************************
15
16      This file is part of the XSL DocBook Stylesheet distribution.
17      See ../README or http://docbook.sf.net/release/xsl/current/ for
18      copyright and other information.
19
20      ******************************************************************** -->
21
22 <!-- put an xml:base attribute on the root element -->
23 <xsl:template match="/*" mode="stripNS">
24   <xsl:choose>
25     <xsl:when test="self::ng:* or self::db:*">
26       <xsl:element name="{local-name(.)}">
27         <xsl:copy-of select="@*[not(name(.) = 'xml:id')
28                                 and not(name(.) = 'version')]"/>
29         <xsl:if test="@xml:id">
30           <xsl:attribute name="id">
31             <xsl:value-of select="@xml:id"/>
32           </xsl:attribute>
33         </xsl:if>
34
35         <xsl:call-template name="add-xml-base"/>
36
37         <xsl:apply-templates mode="stripNS"/>
38       </xsl:element>
39     </xsl:when>
40     <xsl:otherwise>
41       <xsl:copy>
42         <xsl:copy-of select="@*[not(name(.) = 'xml:id')
43                                 and not(name(.) = 'version')]"/>
44         <xsl:if test="@xml:id">
45           <xsl:attribute name="id">
46             <xsl:value-of select="@xml:id"/>
47           </xsl:attribute>
48         </xsl:if>
49
50         <xsl:call-template name="add-xml-base"/>
51
52         <xsl:apply-templates mode="stripNS"/>
53       </xsl:copy>
54     </xsl:otherwise>
55   </xsl:choose>
56 </xsl:template>
57
58 <xsl:template match="*" mode="stripNS">
59   <xsl:choose>
60     <xsl:when test="self::ng:* or self::db:*">
61       <xsl:element name="{local-name(.)}">
62         <xsl:copy-of select="@*[not(name(.) = 'xml:id')
63                                 and not(name(.) = 'version')]"/>
64         <xsl:if test="@xml:id">
65           <xsl:attribute name="id">
66             <xsl:value-of select="@xml:id"/>
67           </xsl:attribute>
68         </xsl:if>
69         <xsl:apply-templates mode="stripNS"/>
70       </xsl:element>
71     </xsl:when>
72     <xsl:otherwise>
73       <xsl:copy>
74         <xsl:copy-of select="@*[not(name(.) = 'xml:id')
75                                 and not(name(.) = 'version')]"/>
76         <xsl:if test="@xml:id">
77           <xsl:attribute name="id">
78             <xsl:value-of select="@xml:id"/>
79           </xsl:attribute>
80         </xsl:if>
81         <xsl:apply-templates mode="stripNS"/>
82       </xsl:copy>
83     </xsl:otherwise>
84   </xsl:choose>
85 </xsl:template>
86
87 <xsl:template match="db:info" mode="stripNS">
88   <xsl:variable name="info">
89     <xsl:choose>
90       <xsl:when test="parent::db:article
91                       |parent::db:appendix
92                       |parent::db:bibliography
93                       |parent::db:book
94                       |parent::db:chapter
95                       |parent::db:glossary
96                       |parent::db:index
97                       |parent::db:part
98                       |parent::db:preface
99                       |parent::db:refentry
100                       |parent::db:reference
101                       |parent::db:refsect1
102                       |parent::db:refsect2
103                       |parent::db:refsect3
104                       |parent::db:refsection
105                       |parent::db:refsynopsisdiv
106                       |parent::db:sect1
107                       |parent::db:sect2
108                       |parent::db:sect3
109                       |parent::db:sect4
110                       |parent::db:sect5
111                       |parent::db:section
112                       |parent::db:setindex
113                       |parent::db:set
114                       |parent::db:slides
115                       |parent::db:sidebar">
116         <xsl:value-of select="local-name(parent::*)"/>
117         <xsl:text>info</xsl:text>
118       </xsl:when>
119       <xsl:when test="parent::db:audioobject
120                       |parent::db:imageobject
121                       |parent::db:inlinemediaobject
122                       |parent::db:mediaobject
123                       |parent::db:mediaobjectco
124                       |parent::db:textobject
125                       |parent::db:videoobject">
126         <xsl:text>objectinfo</xsl:text>
127       </xsl:when>
128       <xsl:otherwise>blockinfo</xsl:otherwise>
129     </xsl:choose>
130   </xsl:variable>
131
132   <xsl:element name="{$info}">
133     <xsl:copy-of select="@*[not(name(.) = 'xml:id')
134                          and not(name(.) = 'version')]"/>
135     <xsl:if test="@xml:id">
136       <xsl:attribute name="id">
137         <xsl:value-of select="@xml:id"/>
138       </xsl:attribute>
139     </xsl:if>
140     <xsl:apply-templates mode="stripNS"/>
141   </xsl:element>
142
143   <xsl:if test="(not(../db:title) and not(../ng:title))
144                 and ($info = 'prefaceinfo'
145                      or $info = 'chapterinfo'
146                      or $info = 'sectioninfo'
147                      or $info = 'sect1info'
148                      or $info = 'sect2info'
149                      or $info = 'sect3info'
150                      or $info = 'sect4info'
151                      or $info = 'sect5info'
152                      or $info = 'refsectioninfo'
153                      or $info = 'refsect1info'
154                      or $info = 'refsect2info'
155                      or $info = 'refsect3info'
156                      or $info = 'blockinfo'
157                      or $info = 'appendixinfo')">
158     <xsl:apply-templates select="db:title|ng:title" mode="stripNS"/>
159   </xsl:if>
160
161 </xsl:template>
162
163 <xsl:template match="ng:tag|db:tag" mode="stripNS">
164   <xsl:choose>
165     <xsl:when test="@xlink:href">
166       <ulink url="{@xlink:href}">
167         <sgmltag>
168           <xsl:copy-of select="@*[not(name(.) = 'xml:id')
169                                and not(name(.) = 'version')
170                                and not(local-name(.) = 'href')]"/>
171           <xsl:apply-templates mode="stripNS"/>
172         </sgmltag>
173       </ulink>
174     </xsl:when>
175     <xsl:otherwise>
176       <sgmltag>
177         <xsl:copy-of select="@*[not(name(.) = 'xml:id')
178                              and not(name(.) = 'version')]"/>
179         <xsl:apply-templates mode="stripNS"/>
180       </sgmltag>
181     </xsl:otherwise>
182   </xsl:choose>
183 </xsl:template>
184
185 <xsl:template match="db:link[@xlink:href]" mode="stripNS">
186   <ulink url="{@xlink:href}">
187     <xsl:if test="@role">
188       <xsl:attribute name="role">
189         <xsl:value-of select="@role"/>
190       </xsl:attribute>
191     </xsl:if>
192     <xsl:apply-templates mode="stripNS"/>
193   </ulink>
194 </xsl:template>
195
196 <xsl:template match="db:citetitle[@xlink:href]" mode="stripNS">
197   <ulink url="{@xlink:href}">
198     <citetitle>
199       <xsl:copy-of select="@*[not(name(.) = 'xml:id')
200                            and not(name(.) = 'version')
201                            and not(local-name(.) = 'href')]"/>
202       <xsl:apply-templates mode="stripNS"/>
203     </citetitle>
204   </ulink>
205 </xsl:template>
206
207 <xsl:template match="db:citetitle[@linkend]" mode="stripNS">
208   <citetitle>
209     <xsl:copy-of select="@*[not(name(.) = 'xml:id')
210                          and not(name(.) = 'version')
211                          and not(name(.) = 'linkend')
212                          and not(local-name(.) = 'href')]"/>
213       <xsl:apply-templates mode="stripNS"/>
214   </citetitle>
215 </xsl:template>
216
217 <xsl:template match="db:alt" mode="stripNS"/>
218
219 <xsl:template match="ng:textdata|db:textdata
220                      |ng:imagedata|db:imagedata
221                      |ng:videodata|db:videodata
222                      |ng:audiodata|db:audiodata" mode="stripNS">
223   <xsl:element name="{local-name(.)}">
224     <xsl:copy-of select="@*[not(name(.) = 'xml:id')
225                             and not(name(.) = 'version')
226                             and not(name(.) = 'entityref')]"/>
227     <xsl:if test="@xml:id">
228       <xsl:attribute name="id">
229         <xsl:value-of select="@xml:id"/>
230       </xsl:attribute>
231     </xsl:if>
232
233     <xsl:choose>
234       <xsl:when test="@entityref">
235         <xsl:attribute name="fileref">
236           <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
237         </xsl:attribute>
238       </xsl:when>
239     </xsl:choose>
240
241     <xsl:apply-templates mode="stripNS"/>
242   </xsl:element>
243 </xsl:template>
244
245 <xsl:template name="add-xml-base">
246   <xsl:if test="not(@xml:base)">
247     <xsl:variable name="base">
248       <xsl:choose>
249         <xsl:when test="function-available('saxon:systemId')">
250           <xsl:value-of select="saxon:systemId()"/>
251         </xsl:when>
252         <xsl:when test="function-available('NodeInfo:systemId')">
253           <xsl:value-of select="NodeInfo:systemId()"/>
254         </xsl:when>
255         <xsl:otherwise>
256           <xsl:message>
257             <xsl:text>WARNING: cannot add @xml:base to node </xsl:text>
258             <xsl:text>set root element.  </xsl:text>
259             <xsl:text>Relative paths may not work.</xsl:text>
260           </xsl:message>
261         </xsl:otherwise>
262       </xsl:choose>
263     </xsl:variable>
264     <!-- debug
265     <xsl:message>base is <xsl:value-of select="$base"/></xsl:message>
266     -->
267     <xsl:if test="$base != ''">
268       <xsl:attribute name="xml:base">
269         <xsl:call-template name="systemIdToBaseURI">
270           <xsl:with-param name="systemId">
271             <!-- file: seems to confuse some processors. -->
272             <xsl:choose>
273               <!-- however, windows paths must use file:///c:/path -->
274               <xsl:when test="starts-with($base, 'file:///') and
275                               substring($base, 10, 1) = ':'">
276                 <xsl:value-of select="$base"/>
277               </xsl:when>
278               <xsl:when test="starts-with($base, 'file:/')
279                               and substring($base, 8, 1) = ':'">
280                 <xsl:value-of select="concat('file:///', 
281                                       substring-after($base,'file:/'))"/>
282               </xsl:when>
283               <xsl:when test="starts-with($base, 'file:///')">
284                 <xsl:value-of select="substring-after($base,'file://')"/>
285               </xsl:when>
286               <xsl:when test="starts-with($base, 'file://')">
287                 <xsl:value-of select="substring-after($base,'file:/')"/>
288               </xsl:when>
289               <xsl:when test="starts-with($base, 'file:/')">
290                 <xsl:value-of select="substring-after($base,'file:')"/>
291               </xsl:when>
292               <xsl:otherwise>
293                 <xsl:value-of select="$base"/>
294               </xsl:otherwise>
295             </xsl:choose>
296           </xsl:with-param>
297         </xsl:call-template>
298       </xsl:attribute>
299     </xsl:if>
300   </xsl:if>
301 </xsl:template>
302
303 <xsl:template name="systemIdToBaseURI">
304   <xsl:param name="systemId" select="''"/>
305   <xsl:if test="contains($systemId,'/')">
306     <xsl:value-of select="substring-before($systemId,'/')"/>
307     <xsl:text>/</xsl:text>
308     <xsl:call-template name="systemIdToBaseURI">
309       <xsl:with-param name="systemId"
310                       select="substring-after($systemId,'/')"/>
311     </xsl:call-template>
312   </xsl:if>
313 </xsl:template>
314
315 <xsl:template match="comment()|processing-instruction()|text()" mode="stripNS">
316   <xsl:copy/>
317 </xsl:template>
318
319 <xsl:template match="/" priority="-1">
320   <!-- need a local version of this variable because this module imported many places-->
321   <xsl:variable name="local.exsl.node.set.available">
322     <xsl:choose>
323       <xsl:when exsl:foo="" xmlns:exsl="http://exslt.org/common"
324         test="function-available('exsl:node-set') or
325                          contains(system-property('xsl:vendor'),
326                            'Apache Software Foundation')">1</xsl:when>
327       <xsl:otherwise>0</xsl:otherwise>
328     </xsl:choose>
329   </xsl:variable>
330   <xsl:choose>
331     <xsl:when test="$local.exsl.node.set.available != 0
332                     and (*/self::ng:* or */self::db:*)">
333       <xsl:message>
334         <xsl:text>Stripping namespace from DocBook 5 document. </xsl:text>
335         <xsl:text>It is suggested to use namespaced version of the stylesheets </xsl:text>
336         <xsl:text>available in distribution file 'docbook-xsl-ns' </xsl:text>
337         <xsl:text>at //http://sourceforge.net/projects/docbook/files/</xsl:text>
338         <xsl:text> which does not require namespace stripping step.</xsl:text>
339       </xsl:message>
340       <xsl:variable name="nons">
341         <xsl:apply-templates mode="stripNS"/>
342       </xsl:variable>
343       <xsl:message>Processing stripped document.</xsl:message>
344       <xsl:apply-templates select="exsl:node-set($nons)"/>
345     </xsl:when>
346     <xsl:otherwise>
347       <xsl:copy-of select="node()"/>
348     </xsl:otherwise>
349   </xsl:choose>
350 </xsl:template>
351
352 </xsl:stylesheet>