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:exsl="http://exslt.org/common"
7                exclude-result-prefixes="db ng exsl saxon"
8                version='1.0'>
9
10<!-- ********************************************************************
11     $Id: stripns.xsl,v 1.12 2006/05/02 16:15:29 bobstayton Exp $
12     ********************************************************************
13
14     This file is part of the XSL DocBook Stylesheet distribution.
15     See /README or http://nwalsh.com/docbook/xsl/ for copyright
16     and other information.
17
18     This file contains general templates common to both the HTML and FO
19     versions of the DocBook stylesheets.
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:link|db:link" mode="stripNS">
164  <xsl:variable xmlns:xlink="http://www.w3.org/1999/xlink"
165		name="href" select="@xlink:href|@href"/>
166  <xsl:choose>
167    <xsl:when test="$href != '' and not(starts-with($href,'#'))">
168      <ulink url="{$href}">
169	<xsl:for-each select="@*">
170	  <xsl:if test="local-name(.) != 'href'
171			and name(.) != 'version'
172			and name(.) != 'xml:id'">
173	    <xsl:copy/>
174	  </xsl:if>
175	</xsl:for-each>
176	<xsl:if test="@xml:id">
177	  <xsl:attribute name="id">
178	    <xsl:value-of select="@xml:id"/>
179	  </xsl:attribute>
180	</xsl:if>
181	<xsl:apply-templates mode="stripNS"/>
182      </ulink>
183    </xsl:when>
184    <xsl:when test="$href != '' and starts-with($href,'#')">
185      <link linkend="{substring-after($href,'#')}">
186	<xsl:for-each select="@*">
187	  <xsl:if test="local-name(.) != 'href'
188			and name(.) != 'version'
189			and name(.) != 'xml:id'">
190	    <xsl:copy/>
191	  </xsl:if>
192	</xsl:for-each>
193	<xsl:if test="@xml:id">
194	  <xsl:attribute name="id">
195	    <xsl:value-of select="@xml:id"/>
196	  </xsl:attribute>
197	</xsl:if>
198	<xsl:apply-templates mode="stripNS"/>
199      </link>
200    </xsl:when>
201    <xsl:otherwise>
202      <link>
203	<xsl:copy-of select="@*[not(name(.) = 'xml:id')
204			     and not(name(.) = 'version')]"/>
205	<xsl:if test="@xml:id">
206	  <xsl:attribute name="id">
207	    <xsl:value-of select="@xml:id"/>
208	  </xsl:attribute>
209	</xsl:if>
210	<xsl:apply-templates mode="stripNS"/>
211      </link>
212    </xsl:otherwise>
213  </xsl:choose>
214</xsl:template>
215
216<xsl:template match="ng:tag|db:tag" mode="stripNS">
217  <sgmltag>
218    <xsl:copy-of select="@*[not(name(.) = 'xml:id')
219			 and not(name(.) = 'version')]"/>
220    <xsl:apply-templates mode="stripNS"/>
221  </sgmltag>
222</xsl:template>
223
224<xsl:template match="ng:textdata|db:textdata
225		     |ng:imagedata|db:imagedata
226		     |ng:videodata|db:videodata
227		     |ng:audiodata|db:audiodata" mode="stripNS">
228  <xsl:element name="{local-name(.)}">
229    <xsl:copy-of select="@*[not(name(.) = 'xml:id')
230			    and not(name(.) = 'version')
231			    and not(name(.) = 'entityref')]"/>
232    <xsl:if test="@xml:id">
233      <xsl:attribute name="id">
234	<xsl:value-of select="@xml:id"/>
235      </xsl:attribute>
236    </xsl:if>
237
238    <xsl:choose>
239      <xsl:when test="@entityref">
240	<xsl:attribute name="fileref">
241	  <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
242	</xsl:attribute>
243      </xsl:when>
244    </xsl:choose>
245
246    <xsl:apply-templates mode="stripNS"/>
247  </xsl:element>
248</xsl:template>
249
250<xsl:template name="add-xml-base">
251  <xsl:if test="not(@xml:base) and function-available('saxon:systemId')">
252    <xsl:variable name="base" select="saxon:systemId()"/>
253    <xsl:attribute name="xml:base">
254      <xsl:call-template name="systemIdToBaseURI">
255	<xsl:with-param name="systemId">
256	  <!-- file: seems to confuse some processors. -->
257	  <xsl:choose>
258	    <xsl:when test="starts-with($base, 'file:///')">
259	      <xsl:value-of select="substring-after($base,'file://')"/>
260	    </xsl:when>
261	    <xsl:when test="starts-with($base, 'file://')">
262	      <xsl:value-of select="substring-after($base,'file:/')"/>
263	    </xsl:when>
264	    <xsl:when test="starts-with($base, 'file:/')
265			    and substring($base, 8, 1) = ':'">
266	      <!-- Stupid windows file:/c:/path... -->
267	      <xsl:value-of select="concat('file:///', substring-after($base,'file:/'))"/>
268	    </xsl:when>
269	    <xsl:when test="starts-with($base, 'file:/')">
270	      <xsl:value-of select="substring-after($base,'file:')"/>
271	    </xsl:when>
272	    <xsl:otherwise>
273	      <xsl:value-of select="$base"/>
274	    </xsl:otherwise>
275	  </xsl:choose>
276	</xsl:with-param>
277      </xsl:call-template>
278    </xsl:attribute>
279  </xsl:if>
280</xsl:template>
281
282<xsl:template name="systemIdToBaseURI">
283  <xsl:param name="systemId" select="''"/>
284  <xsl:if test="contains($systemId,'/')">
285    <xsl:value-of select="substring-before($systemId,'/')"/>
286    <xsl:text>/</xsl:text>
287    <xsl:call-template name="systemIdToBaseURI">
288      <xsl:with-param name="systemId"
289		      select="substring-after($systemId,'/')"/>
290    </xsl:call-template>
291  </xsl:if>
292</xsl:template>
293
294<xsl:template match="comment()|processing-instruction()|text()" mode="stripNS">
295  <xsl:copy/>
296</xsl:template>
297
298<xsl:template match="/" priority="-1">
299  <xsl:choose>
300    <xsl:when test="function-available('exsl:node-set')
301                    and (*/self::ng:* or */self::db:*)">
302      <xsl:message>Stripping NS from DocBook 5/NG document.</xsl:message>
303      <xsl:variable name="nons">
304        <xsl:apply-templates mode="stripNS"/>
305      </xsl:variable>
306      <xsl:message>Processing stripped document.</xsl:message>
307      <xsl:apply-templates select="exsl:node-set($nons)"/>
308    </xsl:when>
309    <xsl:otherwise>
310      <xsl:copy-of select="@* | node()"/>
311    </xsl:otherwise>
312  </xsl:choose>
313</xsl:template>
314
315</xsl:stylesheet>
316