Whamcloud - gitweb
LUDOC-120 presentation: build bits for webhelp.
[doc/manual.git] / webhelp / 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 9016 2011-06-07 12:09:34Z nwalsh $
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:if test="@xml:id">
172             <xsl:attribute name="id">
173               <xsl:value-of select="@xml:id"/>
174             </xsl:attribute>
175           </xsl:if>
176           <xsl:apply-templates mode="stripNS"/>
177         </sgmltag>
178       </ulink>
179     </xsl:when>
180     <xsl:otherwise>
181       <sgmltag>
182         <xsl:copy-of select="@*[not(name(.) = 'xml:id')
183                                 and not(name(.) = 'version')]"/>
184           <xsl:if test="@xml:id">
185             <xsl:attribute name="id">
186               <xsl:value-of select="@xml:id"/>
187             </xsl:attribute>
188           </xsl:if>
189         <xsl:apply-templates mode="stripNS"/>
190       </sgmltag>
191     </xsl:otherwise>
192   </xsl:choose>
193 </xsl:template>
194
195 <xsl:template match="db:link[@xlink:href]" mode="stripNS">
196   <ulink url="{@xlink:href}">
197     <xsl:if test="@role">
198       <xsl:attribute name="role">
199         <xsl:value-of select="@role"/>
200       </xsl:attribute>
201     </xsl:if>
202     <xsl:apply-templates mode="stripNS"/>
203   </ulink>
204 </xsl:template>
205
206 <xsl:template match="db:citetitle[@xlink:href]" mode="stripNS">
207   <ulink url="{@xlink:href}">
208     <citetitle>
209       <xsl:copy-of select="@*[not(name(.) = 'xml:id')
210                            and not(name(.) = 'version')
211                            and not(local-name(.) = 'href')]"/>
212       <xsl:if test="@xml:id">
213         <xsl:attribute name="id">
214           <xsl:value-of select="@xml:id"/>
215         </xsl:attribute>
216       </xsl:if>
217       <xsl:apply-templates mode="stripNS"/>
218     </citetitle>
219   </ulink>
220 </xsl:template>
221
222 <xsl:template match="db:citetitle[@linkend]" mode="stripNS">
223   <citetitle>
224     <xsl:copy-of select="@*[not(name(.) = 'xml:id')
225                          and not(name(.) = 'version')
226                          and not(name(.) = 'linkend')
227                          and not(local-name(.) = 'href')]"/>
228     <xsl:if test="@xml:id">
229       <xsl:attribute name="id">
230         <xsl:value-of select="@xml:id"/>
231       </xsl:attribute>
232     </xsl:if>
233       <xsl:apply-templates mode="stripNS"/>
234   </citetitle>
235 </xsl:template>
236
237 <xsl:template match="db:alt" mode="stripNS"/>
238
239 <xsl:template match="ng:textdata|db:textdata
240                      |ng:imagedata|db:imagedata
241                      |ng:videodata|db:videodata
242                      |ng:audiodata|db:audiodata" mode="stripNS">
243   <xsl:element name="{local-name(.)}">
244     <xsl:copy-of select="@*[not(name(.) = 'xml:id')
245                             and not(name(.) = 'version')
246                             and not(name(.) = 'entityref')]"/>
247     <xsl:if test="@xml:id">
248       <xsl:attribute name="id">
249         <xsl:value-of select="@xml:id"/>
250       </xsl:attribute>
251     </xsl:if>
252
253     <xsl:choose>
254       <xsl:when test="@entityref">
255         <xsl:attribute name="fileref">
256           <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
257         </xsl:attribute>
258       </xsl:when>
259     </xsl:choose>
260
261     <xsl:apply-templates mode="stripNS"/>
262   </xsl:element>
263 </xsl:template>
264
265 <xsl:template name="add-xml-base">
266   <xsl:if test="not(@xml:base)">
267     <xsl:variable name="base">
268       <xsl:choose>
269         <xsl:when test="function-available('saxon:systemId')">
270           <xsl:value-of select="saxon:systemId()"/>
271         </xsl:when>
272         <xsl:when test="function-available('NodeInfo:systemId')">
273           <xsl:value-of select="NodeInfo:systemId()"/>
274         </xsl:when>
275         <xsl:otherwise>
276           <xsl:message>
277             <xsl:text>WARNING: cannot add @xml:base to node </xsl:text>
278             <xsl:text>set root element.  </xsl:text>
279             <xsl:text>Relative paths may not work.</xsl:text>
280           </xsl:message>
281         </xsl:otherwise>
282       </xsl:choose>
283     </xsl:variable>
284     <!-- debug
285     <xsl:message>base is <xsl:value-of select="$base"/></xsl:message>
286     -->
287     <xsl:if test="$base != ''">
288       <xsl:attribute name="xml:base">
289         <xsl:call-template name="systemIdToBaseURI">
290           <xsl:with-param name="systemId">
291             <!-- file: seems to confuse some processors. -->
292             <xsl:choose>
293               <!-- however, windows paths must use file:///c:/path -->
294               <xsl:when test="starts-with($base, 'file:///') and
295                               substring($base, 10, 1) = ':'">
296                 <xsl:value-of select="$base"/>
297               </xsl:when>
298               <xsl:when test="starts-with($base, 'file:/')
299                               and substring($base, 8, 1) = ':'">
300                 <xsl:value-of select="concat('file:///', 
301                                       substring-after($base,'file:/'))"/>
302               </xsl:when>
303               <xsl:when test="starts-with($base, 'file:///')">
304                 <xsl:value-of select="substring-after($base,'file://')"/>
305               </xsl:when>
306               <xsl:when test="starts-with($base, 'file://')">
307                 <xsl:value-of select="substring-after($base,'file:/')"/>
308               </xsl:when>
309               <xsl:when test="starts-with($base, 'file:/')">
310                 <xsl:value-of select="substring-after($base,'file:')"/>
311               </xsl:when>
312               <xsl:otherwise>
313                 <xsl:value-of select="$base"/>
314               </xsl:otherwise>
315             </xsl:choose>
316           </xsl:with-param>
317         </xsl:call-template>
318       </xsl:attribute>
319     </xsl:if>
320   </xsl:if>
321 </xsl:template>
322
323 <xsl:template name="systemIdToBaseURI">
324   <xsl:param name="systemId" select="''"/>
325   <xsl:if test="contains($systemId,'/')">
326     <xsl:value-of select="substring-before($systemId,'/')"/>
327     <xsl:text>/</xsl:text>
328     <xsl:call-template name="systemIdToBaseURI">
329       <xsl:with-param name="systemId"
330                       select="substring-after($systemId,'/')"/>
331     </xsl:call-template>
332   </xsl:if>
333 </xsl:template>
334
335 <xsl:template match="comment()|processing-instruction()|text()" mode="stripNS">
336   <xsl:copy/>
337 </xsl:template>
338
339 <xsl:template match="/" priority="-1">
340   <!-- need a local version of this variable because this module imported many places-->
341   <xsl:variable name="local.exsl.node.set.available">
342     <xsl:choose>
343       <xsl:when exsl:foo="" xmlns:exsl="http://exslt.org/common"
344         test="function-available('exsl:node-set') or
345                          contains(system-property('xsl:vendor'),
346                            'Apache Software Foundation')">1</xsl:when>
347       <xsl:otherwise>0</xsl:otherwise>
348     </xsl:choose>
349   </xsl:variable>
350   <xsl:choose>
351     <xsl:when test="$local.exsl.node.set.available != 0
352                     and (*/self::ng:* or */self::db:*)">
353       <xsl:message>
354         <xsl:text>Stripping namespace from DocBook 5 document. </xsl:text>
355         <xsl:text>It is suggested to use namespaced version of the stylesheets </xsl:text>
356         <xsl:text>available in distribution file 'docbook-xsl-ns' </xsl:text>
357         <xsl:text>at //http://sourceforge.net/projects/docbook/files/</xsl:text>
358         <xsl:text> which does not require namespace stripping step.</xsl:text>
359       </xsl:message>
360       <xsl:variable name="nons">
361         <xsl:apply-templates mode="stripNS"/>
362       </xsl:variable>
363       <xsl:message>Processing stripped document.</xsl:message>
364       <xsl:apply-templates select="exsl:node-set($nons)"/>
365     </xsl:when>
366     <xsl:otherwise>
367       <xsl:copy-of select="node()"/>
368     </xsl:otherwise>
369   </xsl:choose>
370 </xsl:template>
371
372 </xsl:stylesheet>